Caching Array Length

JavaScript performance comparison

Revision 25 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var theArray = ["", 56,
      function() {},
      93, "lalala", {}
    ];
    var copyOfTheArray = [];
};
</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
Not defined
for (var i = 0; i < theArray.length; i++) {
  copyOfTheArray[i] = theArray[i];
}
pending…
Pre defined
var i, len = theArray.length;
for (i = 0; i < len; i++) {
  copyOfTheArray[i] = theArray[i];
}
pending…
Inline define
for (var i = 0, len = theArray.length; i < len; i++) {
  copyOfTheArray[i] = theArray[i];
}
pending…
forEach
function doAppend(item, index) {
  copyOfTheArray[index] = item;
}
theArray.forEach(doAppend);
pending…
forEach anon
theArray.forEach(function(item, index) {
  copyOfTheArray[index] = item;
});
pending…
Map
copyOfTheArray = theArray.map(function(item) {
  return item;
});
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