Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why push method is significantly slower than putting values via array indices in Javascript

I pretty don't understand why this test :

http://jsperf.com/push-method-vs-setting-via-key

Shows that

 a.push(Math.random());

is over ten times slower than

 a[i] = Math.random();

Could you explain why this is the case ? What magic "push" do that make it so slow ? (or so slow compared to other valid method of doing that).

EDIT

NOTE: The push test is biased. I increase size of the array every iteration! Read carefully accepted answer!

Benchmark results

like image 989
Paul Brewczynski Avatar asked Jan 10 '14 01:01

Paul Brewczynski


People also ask

Is push faster than Unshift?

Unshift is slower than push because it also needs to unshift all the elements to the left once the first element is added.

What does push () method of the array do?

The push() method adds one or more elements to the end of an array and returns the new length of the array.

What is the difference between push and Unshift in JavaScript?

The Complete Full-Stack JavaScript Course! Both the methods are used to add elements to the array. But the only difference is unshift() method adds the element at the start of the array whereas push() adds the element at the end of the array.

Is concat faster than push?

concat performs at 0.40 ops/sec, while . push performs at 378 ops/sec. push is 945x faster than concat ! This difference might not be linear, but it is already is already significant at this small scale.


2 Answers

Could you explain why this is the case?

Because your test is flawed. The push does always append to the existing a array making it much larger, while the second test does only use the first 1000 indices. Using the setup is not enough here, you would have to reset the a array before every for-loop: http://jsperf.com/push-method-vs-setting-via-key/3.

Apart from that, the method call to push might have a little overhead, and determining the current array length might need additional time compared to using the index of the for-loop.

Usually there is no reason not to use push - the method is there for exactly that operation and makes some code easier to read. While a few people think one version is faster than the other, both are equally optimized in browsers. See Why is array.push sometimes faster than array[n] = value? and Using the push method or .length when adding to array? - results vary so wide that it's actually irrelevant. Use what is better to understand.

like image 125
Bergi Avatar answered Sep 18 '22 23:09

Bergi


That's simply because Google decided to put more work into optimising array indexing than optimising the push method in Chrome.

If you look at the test results now that a few more people have tried it, you see that the performance differes quite a lot between different browsers, and even between different versions of the same browser.

Nowadays browsers compile the Javascript code, which means that the browser turns the code into something that is much faster to run that interpreted Javascript. What the compiler does with the code determines how different ways of doing things performs. Different compilers optimise certain things better, which gives the different preformances.

like image 26
Guffa Avatar answered Sep 20 '22 23:09

Guffa