Object iteration with pre-compiled iterator (v4)

Revision 4 of this benchmark created on


Preparation HTML

<script>

</script>

Setup

/**
     * Compile iterator function for a specific type.
     */
    var compileIterator = function(typeProperties) {
      // pre-compile constant iteration over object properties
      var iteratorFunStr = '(function(obj, cb) {\n';
      for (var i = 0; i < typeProperties.length; ++i) {
        iteratorFunStr += 'cb(obj.' + typeProperties[i] + ');\n';
      };
      iteratorFunStr += '})';
    
      // actually compile and return the function
      return eval(iteratorFunStr);
    };
    
    
    // now, we can build custom type descriptions of varying sizes
    var NObjectSize = 30000;
    
    // create a custom test "type property set"
    var testTypeProperties = [];
    for (var i = 1; i <= NObjectSize; ++i) {
      testTypeProperties.push('a' + i); // non-numeric properties to be on the safe side
    };
    
    
    // compile iterator function
    var iteratorFun = compileIterator(testTypeProperties);
    
    // create a test object
    var createTestObject = function() {
      var objString = '({'
      for (var i = 0; i < testTypeProperties.length; ++i) {
        objString += testTypeProperties[i] + ': ' + (i + 1) + ',\n';
      }
      objString += '});';
    
      return eval(objString);
    };
    
    var o1 = createTestObject();
    var o2 = createTestObject();
    
    var aFunction = function(total, next) {
      return total * next + 1;
    };

Test runner

Ready to run.

Testing in
TestOps/sec
for ... in (sum)
var x = 0;
for (var p in o1) {
  x += o1[p];
}
ready
Pre-compiled iterator (sum)
var x = 0;
var iteratorCb = function(value) {
  x += value;
};
iteratorFun(o2, iteratorCb);
ready
for ... in (copy)
var x = [];
for (var p in o1) {
  x.push(o1[p]);
}
ready
Pre-compiled iterator (copy)
var x = [];
var iteratorCb = function(value) {
  x.push(value);
};
iteratorFun(o2, iteratorCb);
ready
for ... in (function call)
var x = 0;
for (var p in o1) {
  x += aFunction(x, o1[p]);
}
ready
Pre-compiled iterator (function call)
var x = 0;
var iteratorCb = function(value) {
  x += aFunction(x, value);
};
iteratorFun(o2, iteratorCb);
ready

Revisions

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