Array push() vs unshift() vs direct assignment vs direct assignment out-of-order

JavaScript performance comparison

Revision 9 of this test case created by Jason Kuhrt

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var a = [];
    var b = new Array(5);
   
};
</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
a.push(1);
a.push(2);
a.push(3);
a.push(4);
a.push(5);
a.push(6);
pending…
unshift
a.unshift(6);
a.unshift(5);
a.unshift(4);
a.unshift(3);
a.unshift(2);
a.unshift(1);
pending…
Direct Assignment
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;
a[5] = 6;
 
pending…
Fixed array
b[0] = 1;
b[1] = 2;
b[2] = 3;
b[3] = 4;
b[4] = 5;
b[5] = 6;
 
pending…
Direct with length
a[a.length] = 1;
a[a.length] = 2;
a[a.length] = 3;
a[a.length] = 4;
a[a.length] = 5;
a[a.length] = 6;
 
pending…
Fixed array (out of order assignment)
b[1] = 2;
b[3] = 4;
b[0] = 1;
b[2] = 3;
b[5] = 6;
b[4] = 5;

 
pending…
Direct Assignment (out of order assignment)
a[1] = 2;
a[3] = 4;
a[0] = 1;
a[2] = 3;
a[5] = 6;
a[4] = 5;

 
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