array.push vs array[0]

JavaScript performance comparison

Revision 2 of this test case created by knopsh

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
a.push
var a = [];
a.push(4);
pending…
b.push
var b = [ 1, 2, 3 ];
b.push(4);
 
pending…
a[0] = 4
var a = [];
a[0] = 4;
pending…
a.length ? a.push(4) : a[0] = 4
var a = [];
a.length ? a.push(4) : a[0] = 4;
pending…
b.length ? b.push(4) : b[0] = 4
var b = [ 1, 2, 3 ];
b.length ? b.push(4) : b[0] = 4;
pending…
a[ a.length ] = 4
var a = [];
a[ a.length ] = 4;
pending…
b[ b.length ] = 4
var b = [ 1, 2, 3 ];
b[ b.length ] = 4;
pending…
if (a.length) { a.push(4) } else { a[0] = 4 }
var a = [];
if (a.length) { a.push(4) } else { a[0] = 4 }
pending…
if (b.length) { b.push(4) } else { b[0] = 4 }
var b = [ 1, 2, 3 ];
if (b.length) { b.push(4) } else { b[0] = 4 }
pending…
b[4] = 4
var b = [ 1, 2, 3 ];
b[3] = 4;
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