caching length

JavaScript performance comparison

Revision 38 of this test case created by Mitranim

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = [];
    for (var i = 0; i < 1000; i++) {
      arr[i] = Math.random();
    }
};
</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
for cached
for (var i = 0, ii = arr.length; i < ii; ++i) {
  arr[i] *= 2;
}
pending…
for uncached
for (var i = 0; i < arr.length; ++i) {
  arr[i] *= 2;
}
pending…
while cached
var i = -1, ii = arr.length;
while (++i < ii) {
  arr[i] *= 2;
}
pending…
while uncached
var i = -1;
while (++i < arr.length) {
  arr[i] *= 2;
}
pending…
while cached reverse
var i = arr.length;
while (--i >= 0) {
  arr[i] *= 2;
}
pending…
do-while cached
var i = 0, ii = arr.length;
do {
  arr[i] *= 2;
} while (++i < ii);
pending…
do-while uncached
var i = 0;
do {
  arr[i] *= 2;
} while (++i < arr.length);
pending…
Array forEach
arr.forEach(function(value, i) {
  arr[i] = value * 2;
});
pending…
for..in
for (var i in arr) {
  arr[i] *= 2;
}
pending…
for with function
for (var i = 0, ii = arr.length; i < ii; ++i) {
  step(i);
}
function step(i) {
  arr[i] *= 2;
}
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

Comment form temporarily disabled.

Add a comment