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:
- Why
shiftfunction become a constant complexity (all time is 0) ? - In
unshiftwhy 10k length is somewhat slower than 100k and 1 million ? - Is it because of the underlying machine using zig instead of V8 so it have different behavior ?