Push vs Length

JavaScript performance comparison

Test case created by James Morrin

Info

Which is faster Array.push(val) or Array[Array.length] = val?

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var collector = [],
        maxLength = 10000,
        idx;
};
</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
Array.push(val)
collector = [];
for (idx = 0; idx < maxLength; idx++) {
  collector.push(Math.random());
}
pending…
Array[Array.length] = val
collector = [];
for (idx = 0; idx < maxLength; idx++) {
  collector[collector.length] = Math.random();
}
pending…
Array[idx] = val
collector = [];
for (idx = 0; idx < maxLength; idx++) {
  collector[idx] = Math.random();
}
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