Reference indexed object in loop or variable

JavaScript performance comparison

Test case created by Rich

Preparation code

<script>
  function GenericObject(mType, mKey) {
   this.id = 1;
   this.type = mType;
   this.typedArray = mType + "=" + mType + "="
   mKey + "=" + this.id;
   this.keyId = mKey;
  }
  
  var array = new Array();
  for (var i = 0; i < 1000; i++) {
   if (i % 2 == 0) {
    array[i] = new GenericObject('a', i);
   } else {
    array[i] = new GenericObject('b', i);
   }
  }
  
  var array1a = new Array();
  var array1b = new Array();
  var array1c = new Array();
  var array1d = new Array();
</script>
    

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Reference the object throughout.
for (var array_x = 0, arraylen = array.length; array_x < arraylen; array_x++) {
 if (array[array_x].type == 'a') {
  array1a.push(array[array_x].type + ' ' + array[array_x].keyId);
 } else {
  array1a.push(array[array_x].type + ' ' + array[array_x].keyId);
 }
}
pending…
Reference variables of the object.
for (var array_x = 0, arraylen = array.length; array_x < arraylen; array_x++) {
 var t = array[array_x].type,
     k = array[array_x].keyId;
 if (t == 'a') {
  array1b.push(t + ' ' + k);
 } else {
  array1b.push(t + ' ' + k);
 }
}
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