push/pop vs unshift/shift

JavaScript performance comparison

Revision 7 of this test case created by Foo Bar

Info

Comparing the speed / performance of using an array as a stack using either push & pop or unshift & shift. Tests are both raw, and with accessing the last added entry.

Preparation code

<script>
  var pl = ['a', 'b', 'c', 'd'];
  var sl = ['b', 'c', 'd', 'e'];
 
  var e, a, v;
</script>

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/Pop
pl.push('e');
e = pl.pop();
pending…
Unshift/Shift
sl.unshift('a');
a = sl.shift();
pending…
Push/Access/Pop
pl.push('e');
v = pl[4];
e = pl.pop();
pending…
Unshift/Access/Shift
sl.unshift('a');
v = sl[0];
a = sl.shift();
pending…
Push/Shift
pl.push('e');
a = pl.shift();
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:

0 comments

Add a comment