Extend without foreach

JavaScript performance comparison

Test case created by Shane O'Sullivan

Info

Testing underscore's extend() function against a version of it that doesn't use it's each() function, and instead inlines the necessary code

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var slice = Array.prototype.slice;
    var breaker = {};
   
    var each = function(obj, iterator, context) {
      if (obj.length === +obj.length) {
        for (var i = 0, l = obj.length; i < l; i++) {
          if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;
        }
      }
    };
   
    var origExtend = function(obj) {
      each(slice.call(arguments, 1), function(source) {
        for (var prop in source) {
          if (source[prop] !== void 0) obj[prop] = source[prop];
        }
      });
      return obj;
    }
   
    var inlineExtend = function(obj) {
      var len = arguments.length;
      var source;
      var prop;
      for (var i = 1; i < len; i++) {
        source = arguments[i];
        for (prop in source) {
          if (source[prop] !== void 0) obj[prop] = source[prop];
        }
      }
      return obj;
    }
   
    var dest = {};
    var src = {
      a: 'b',
      c: 'd'
    };
};
</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
Extend + each
origExtend(dest, src)
pending…
Extend + inline
inlineExtend(dest, src)
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