Browser Diet - Cache Array Length

JavaScript performance comparison

Revision 21 of this test case created by Resetko

Preparation code

<script>
  var arr = new Array(1000),
      len, i, p, j;
  for (j = 0; j < 1000; j++)
  arr[j] = 1.1;
</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
Without cache
for (i = 0; i < arr.length; i++) {
  p = arr[i];
  arr[i] = p + 1;
}
pending…
With cache
for (i = 0, len = arr.length; i < len; i++) {
  p = arr[i];
  arr[i] = p + 1;
}
pending…
For each
for (var item in arr) {
  p = arr[item];
  arr[item] = p + 1;
}
pending…
Array.forEach
function doSomething(element, index, array) {
  p = array[index];
  array[index] = p + 1;
}
arr.forEach(doSomething);
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