array loop var caching

JavaScript performance comparison

Test case created


Testing speed between array index searching and variable reference.

Data set contains 5000 elements.

Test performs 200 operations with a single index

Preparation code

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

Benchmark.prototype.teardown = function() {
    var data = null;

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];
caching index
var x = data[300];

for (var i = 0; i < 200; i++) {
  // get data from reference
  var y = x;

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:

1 comment

Val Baca commented :

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

Comment form temporarily disabled.

Add a comment