angular.extend vs underscore.extend

JavaScript performance comparison

Revision 6 of this test case created

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.5/angular.min.js">
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var a = {
      a: 1,
      b: 2,
      c: 3
    };
     
    var b = {
      d: 4,
      e: 5,
      f: 6
    };
   
    function cloneMerge(/* object arguments */) {
        var params = {}, i, numArgs, j, numKeys, obj, keys, key;
   
        for (i = 0, numArgs = arguments.length; i < numArgs; i++) {
            obj = arguments[i];
            keys = Object.keys(obj);
   
            for (j = 0, numKeys = keys.length; j < numKeys; j++) {
                key = keys[j];
                params[key] = obj[key];
            }
        }
   
        return params;
    };
   
    function cloneMerge2(/* object arguments */) {
        var params = {}, i, numArgs, obj;
   
        for (i = 0, numArgs = arguments.length; i < numArgs; i++) {
            obj = arguments[i];
           
            Object.keys(obj).forEach(function (key) {
                params[key] = obj[key];
            });
        }
   
        return params;
    };
   
    function cloneMerge3(/* object arguments */) {
        var params = {};
   
        for (i = 0, numArgs = arguments.length; i < numArgs; i++) {
            var obj = arguments[i];
            var keys = Object.keys(obj);
   
            for (j = 0, numKeys = keys.length; j < numKeys; j++) {
                var key = keys[j];
                params[key] = obj[key];
            }
        }
   
        return params;
    };
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
angular.extend()
angular.extend(a, b);
pending…
underscore.extend()
_.extend(a, b);
pending…
raw loops
cloneMerge(a, b);
pending…
raw loops 2
cloneMerge2(a, b);
pending…
raw loops 3
cloneMerge3(a, b);
pending…

Compare results of other browsers

Revisions

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:

0 comments

Add a comment