Array push() vs unshift() vs direct assignment

JavaScript performance comparison

Revision 6 of this test case created

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
var a =[];
a.push(1);
a.push(2);
a.push(3);
a.push(4);
a.push(5);
a.push(6);
pending…
unshift
var a =[];
a.unshift(6);
a.unshift(5);
a.unshift(4);
a.unshift(3);
a.unshift(2);
a.unshift(1);
pending…
Direct Assignment
var a = [];
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;
a[5] = 6;
 
pending…
Fixed array
var a = new Array(5);
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;
a[5] = 6;
 
pending…
Direct with length
var a = [];
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…
Foo
var a = [];
var p = 0;
a[p] = 1;
a[p+1] = 2;
a[p+2] = 3;
a[p+3] = 4;
a[p+4] = 5;
a[p+5] = 6;
 
pending…
Bar
var a = [];
var p = 0;
a[p++] = 1;
a[p++] = 2;
a[p++] = 3;
a[p++] = 4;
a[p++] = 5;
a[p] = 6;
 
pending…
Initialization
var a = [
1,
2,
3,
4,
5,
6
];
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