pop vs shift on a array (v26)

Revision 26 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 arr = new Array(2000).join('*').split('');
var i = arr.length;
while (i--) {
 arr.pop();
}
ready
.shift()
var arr = new Array(2000).join('*').split('');
var i = arr.length;
while (i--) {
 arr.shift();
}
ready
.splice beginning
var arr = new Array(2000).join('*').split('');
var i = arr.length;
while (i--) {
 arr.splice(0, 1);
}
ready
.splice end
var arr = new Array(2000).join('*').split('');
var i = arr.length;
while (i--) {
 arr.splice(i, 1);
}
ready
Decrement .length
var arr = new Array(2000).join('*').split('');
var i = arr.length;
while (i--) {
 arr[i];
 // Beware, setting the length to < 0 will throw an exception
 --arr.length;
}
ready
Reverse pop reverse
var arr = new Array(2000).join('*').split('');
arr.reverse();

var i = arr.length;
while (i--) {
 arr.pop();
}

arr.reverse();
 
ready
unshift
var arr = new Array(2000);
var i = arr.length;
while (i--) {
 arr.unshift('*');
}
ready

Revisions

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