#Weird result testing complexity in JS array for bun vs node

1 messages · Page 1 of 1 (latest)

fair wasp
#

Some question
I just learning DSA from The Primeagen free course in frontend master, in there he use ts-node and node v14
Then there is a part where he tried to determine what type of data structure used in JS arrays. In there, he have script for testing timing for each array operators (get, push, pop, unshift, shift) in growing array size. With this we can get time complexity based on each function timing then determine what kind of data structure it is.

The answer from this test, JS arrays is an arraylist because get, push, pop is a constant while unshift and shift is linear.

Then I follow the test using node v20, it have same result. But when using bun, I have a different kind of result in shift and unshift, it is faster of course, but the time growth is really weird.

// first number is length, second number is time (ms)

//NODE using ts-node
unshift
10 0
100 0
1000 1
10000 3
100000 37
1000000 547
10000000 8515
shift
10 0
100 0
1000 1
10000 2
100000 32
1000000 941
10000000 9560

//BUN run
unshift
10 1
100 0
1000 1
10000 4
100000 2
1000000 0
10000000 52
shift
10 0
100 0
1000 0
10000 0
100000 0
1000000 0
10000000 0

as you can see, bun result is so different than node that it just weirdly confusing. My question will be:

  1. Why shift function become a constant complexity (all time is 0) ?
  2. In unshift why 10k length is somewhat slower than 100k and 1 million ?
  3. Is it because of the underlying machine using zig instead of V8 so it have different behavior ?
#

modifying the test to 100 million resulting in somehow different result again

//bun run
unshift
10 165
100 21
1000 11
10000 21
100000 51
1000000 4
10000000 52
100000000 733
shift
10 4
100 4
1000 0
10000 0
100000 1
1000000 0
10000000 0
100000000 0
potent nexus
#

JS standard doesn't really tell anything about the time complexity of these operations

#

you can implement them with whatever complexity

#

also you should use performance.now() for timing stuff instead of Date.now()

fair wasp
#

But why that the result is behave differently between node and bun?

my understanding, it should have same behavior only faster?
But in bun it have somehow different behaviour than node?

sand socket
#

@fair wasp Try mitata

coral oar
languid sun
#

Like functionally different

#

That might be a webkit thing though

#

And not bun

fair wasp
#

owh the difference between webkit array and v8?

languid sun
#

Idk u could prob look it up

#

If it's just a time difference then it's not really a problem

sand socket
#

There's a 90% chance that you will have to read their C++ code