Array push() vs unshift()

JavaScript performance comparison

Test case created by FredCK

Info

Tests the performance difference of using two different ways of filling arrays at the start. The two TCs produce the same result.

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
push()
var a = [];
a.push(6);
a.push(5);
a.push(4);
a.push(3);
a.push(2);
a.push(1);
a = a.reverse();
pending…
unshift()
var a = [];
a.unshift(6);
a.unshift(5);
a.unshift(4);
a.unshift(3);
a.unshift(2);
a.unshift(1);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

1 comment

Piyush commented :

This is exactly what I was looking for ... I did feel that unshift will be slow since, it needs to change the array index for all elements...

Add a comment