Pre calculating loops

JavaScript performance comparison

Revision 2 of this test case created

Info

Is it worth the effort to calculate the length of the loop in advance?

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 precalculating
var array = ['string1', 'string2', 'string3', 'string4', 'string5', 'string6'],
  newArray = [];

for (var i = 0; i < array.length; i++) {
  newArray.push(array[i]);
}
pending…
with precalculating
var array = ['string1', 'string2', 'string3', 'string4', 'string5', 'string6'],
  newArray = [],
  len = array.length;

for (var i = 0; i < len; i++) {
  newArray.push(array[i]);
}
pending…
in
var array = ['string1', 'string2', 'string3', 'string4', 'string5', 'string6'],
  len = array.length,
  newArray = [];

for (var i in array) {
  newArray.push(array[i]);
}
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