array.push vs array[0]

JavaScript performance comparison

Test case created by knopsh

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var a = [];
    var b = [ 1, 2, 3 ];
    var al = a.length;
    var bl = b.length;
   
};
</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
a.push
a.push(4);
pending…
b.push
b.push(4);
 
pending…
a[0] = 4
a[0] = 4;
pending…
a.length ? a.push(4) : a[0] = 4
a.length ? a.push(4) : a[0] = 4;
pending…
b.length ? b.push(4) : b[0] = 4
b.length ? b.push(4) : b[0] = 4;
pending…
al ? a.push(4) : a[0] = 4
al ? a.push(4) : a[0] = 4;
pending…
bl ? b.push(4) : b[0] = 4
bl ? b.push(4) : b[0] = 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