Merge Objects vs jQuery .Externd()

JavaScript performance comparison

Test case created

Info

Testing speed of js merge object properties script vs using jquery's extend method

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var settings = { validate: false, limit: 5, name: "foo" };
  var options = { validate: true, name: "bar" };
  function merge_options(obj1,obj2){    var obj3 = {};    for (attrname in obj1) { obj3[attrname] = obj1[attrname]; }    for (attrname in obj2) { obj3[attrname] = obj2[attrname]; }    return obj3;}
</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
JavaScript
merge_options(settings, options)
pending…
jQuery
jQuery.extend(settings, options);
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:

1 comment

maven_peace commented :

Before you get tripped up by the results, you should take a look at jQuery's extend code. The call to jQuery's extend utilized in your example is actually overwriting the original settings object.

With this setup, your merge is not really a comparable operation. Switching to "jQuery.extend({},settings,options);" should be more accurate of a test.

https://github.com/jquery/jquery/blob/master/src/core.js#L285

Add a comment