pop vs shift on a array (v24)

Revision 24 of this benchmark created by hiroshi 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.

Preparation HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Auto Link Maker</title>
</head>
<body>
    <a href=" https://itunes.apple.com/us/app/inet-network-scanner/id340793353?at=10lmJ8" target="itunes_store"> inet-network-scanner</a>
<footer>
<script type='text/javascript'>var _merchantSettings=_merchantSettings || [];_merchantSettings.push(['AT', '10lmJ8']);(function(){var     autolink=document.createElement('script');autolink.type='text/javascript';autolink.async=true; autolink.src='https://autolinkmaker.itunes.apple.com/js/itunes_autolinkmaker.js';var s=document.getElementsByTagName('script')[0];s.parentNode.insertBefore(autolink, s);})();</script>
</footer>
</body>
</html>

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(i, 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
Reverse
var array1 = new Array(2000);
array1.reverse();
 
ready
unshift
var array1 = new Array(2000);
var i = array1.length;
while (i--) {
 array1.unshift(1);
}
ready

Revisions

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