pop vs shift on a array (v17)

Revision 17 of this benchmark created on


Description

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

Ready to run.

Testing in
TestOps/sec
.pop()
var array1 = new Array(2000);
var i = array1.length;
while (i--) {
  array1.pop();
}
ready
.shift()
var array1 = new Array(2000);
var i = array1.length;
while (i--) {
  array1.shift();
}
ready
.splice beginning
var array1 = new Array(2000);
var i = array1.length;
while (i--) {
  array1.splice(0, 1);
}
ready
.splice end
var array1 = new Array(2000);
var i = array1.length;
while (i--) {
  array1.splice(-1, 1);
}
ready
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;
}
ready
reversed + .pop()
var array1 = new Array(2000);
var i = array1.length;
array1.reverse();
while (i--) {
  array1.pop();
}
ready
something else
var array1 = new Array(2000);
var i = array1.length;
var o = 0;
while (i--) {
  array1.pop();
}

console.log(o);
ready
pop push
var array1 = new Array(2000);
var i = array1.length;
var o = 0;
while (i--) {
  array1.pop();
  array1.push('yo');
}

console.log(o);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.