array loop var caching

JavaScript performance comparison

Test case created

Info

Testing speed between array index searching and variable reference.

Data set contains 5000 elements.

Test performs 200 operations with a single index

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var data = [];
    var x = new Date();
   
    for (var i = 0; i < 5000; i++) {
      data.push(x.getTime());
    }
};

Benchmark.prototype.teardown = function() {
    var data = null;
};
</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
using index
for (var i = 0; i < 200; i++) {
  // get data at index
  var x = data[300];
}
pending…
caching index
var x = data[300];

for (var i = 0; i < 200; i++) {
  // get data from reference
  var y = x;
}
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:

1 comment

Val Baca commented :

It seems that only certain browsers realize that the loop isn't DOING ANYTHING.

http://www.youtube.com/watch?v=_O1hM-k3aUY

Add a comment