Caching Array Length

JavaScript performance comparison

Revision 55 of this test case created

Preparation code

Benchmark.prototype.setup = function() {
    var theArray = [{a:"asd",b:123},{a:"asd",b:123},
      {a:"asd",b:123}, {a:"asd"}, {a:"asd",b:123},{a:2,b:123},{a:4,b:123},
      {a:"asd",b:123}, {a:"asd"}, {a:"asd",b:123},{b:13123,b:123},{a:13123,b:123},
      {a:"asd",b:123}, {a:"asd"}, {a:"asd",b:123}
    var copyOfTheArray = [];

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
With caching
var len = theArray.length;
for (var i = 0; i < len; i++) {
  copyOfTheArray[i] = theArray[i];
Without caching
for (var i = 0; i < theArray.length; i++) {
  copyOfTheArray[i] = theArray[i];

Compare results of other browsers


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:


Add a comment