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…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment