fast-iter (v2)

Revision 2 of this benchmark created by Emanuel Jackstare on


Preparation HTML

<script src="//jspm.io/system@0.6.js"></script>
<script>
document.getElementById('run').display = 'none';

   System.import('github:codemix/fast.js')
   .then(function(fast) {
      window.fast = fast;
      document.getElementById('run').display = '';
   });

window.megafast = (function() {
  /**
   * Internal helper to bind a function with a known arguments contract
   * to a given context
   *
   * fastBind(function(x) {return this[x];}, {foo: 1}, 1)
   */
  function fastBind(func, thisContext, numArgs) {
    switch (numArgs) {
      case 3:
        return function(a, b, c) {
          return func.call(thisContext, a, b, c);
        };
      case 4:
        return function(a, b, c, d) {
          return func.call(thisContext, a, b, c, d);
        };
    }
    return function() {
      return func.apply(thisContext, arguments);
    };
  }

  return {
    map: function(subject, fn, thisContext) {
      var length = subject.length,
        result = new Array(length),
        i;
      if (arguments.length > 2) {
        fn = fastBind(fn, thisContext, 3);
      }
      for (i = 0; i < length; i++) {
        result[i] = fn(subject[i], i, subject);
      }
      return result;
    },
    concat: function fastConcat () {
      var length = arguments.length,
          arr = [],
          idx = 0, //pointer to the current last index of arr
          i, item, childLength, j;

      for (i = 0; i < length; i++) {
        item = arguments[i];
        if (Array.isArray(item)) {
          childLength = item.length;
          arr.length += childLength;
          for (j = 0; j < childLength; j++) {
            arr[idx++] = item[j];
          }
        }
        else {
          arr[idx++] = item;
        }
      }
      return arr;
    },

    bind: function fastBind (fn, thisContext) {
  var boundLength = arguments.length - 2,
      boundArgs;

  if (boundLength > 0) {
    boundArgs = new Array(boundLength);
    for (var i = 0; i < boundLength; i++) {
      boundArgs[i] = arguments[i + 2];
    }
    return function () {
      var length = arguments.length,
          args = new Array(boundLength + length),
          i;
      for (i = 0; i < boundLength; i++) {
        args[i] = boundArgs[i];
      }
      for (i = 0; i < length; i++) {
        args[boundLength + i] = arguments[i];
      }
      return fn.apply(thisContext, args);
    };
  }
  else {
    return function () {
      return fn.apply(thisContext, arguments);
    };
  }
}
  };

})();
</script>

Setup

var fastBound = fast.bind(function() {
      var that = this;
    }, fast);
    
    var megaBound = megafast.bind(function() {
      var that = this;
    }, fast);
    
    var nativeBound = function() {
      var that = this;
    }.bind(fast);

Test runner

Ready to run.

Testing in
TestOps/sec
fast
fastBound(1, 2, 3);
ready
new
megaBound(1, 2, 3)
ready
Native
nativeBound(1, 2, 3);
ready

Revisions

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