pop vs shift on a array

JavaScript performance comparison

Revision 6 of this test case created by Sander Aarts

Info

If you can choose between a .pop() or .shift() on a Array, what would be a wise decision?

As my http://jsperf.com/adding-items-array/6 test shows the unshift is awful for performance. The same counts for .shift(), awful performance.

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
.pop()
var array1 = new Array(2000);
var i = array1.length;
while (i--) {
 array1.pop();
}
pending…
.shift()
var array1 = new Array(2000);
var i = array1.length;
while (i--) {
 array1.shift();
}
pending…
.splice beginning
var array1 = new Array(2000);
var i = array1.length;
while (i--) {
 array1.splice(0, 1);
}
pending…
.splice end
var array1 = new Array(2000);
var i = array1.length;
while (i--) {
 array1.splice(i, 1);
}
pending…
Decrement .length
var array1 = new Array(2000);
var i = array1.length;
while (i--) {
 array1[i];
 // Beware, setting the length to < 0 will throw an exception
 --array1.length;
}
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

qwerty commented :

The results are wrong because the array is uninitialized. There is huge penalty in v8 when accessing undefined values from uninitialized array. With properly initialized arrays pop() is many times faster than shift().

compare with http://jsperf.com/pop-vs-shift-on-a-array/11

Add a comment