custom underscore extend vs jquery extend (v5)

Revision 5 of this benchmark created on


Description

Testing a custom version of underscore extend without side effects.

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="http://documentcloud.github.com/underscore/underscore-min.js">
</script>
<script>
  var slice = Array.prototype.slice,
      customExtend = function(obj) {
      copy = _.clone(obj);
      _.each(slice.call(arguments, 1), function(source) {
        for (var prop in source) {
          copy[prop] = source[prop];
        }
      });
      return copy;
      },
      options = {
      name: "Dave",
      age: 26,
      family: {
        father: true,
        mother: true
      }
      },
      addlOptions = {
      occupation: "lumberjack",
      education: "phd",
      children: {
        daughters: ['stephanie', 'amanda'],
        sons: ['william']
      }
      };
var aug = function __aug() {
  var args = Array.prototype.slice.call(arguments);
  var deep = false;
  var org = args.shift();
  var type = '';
  if (typeof org === 'string' || typeof org === 'boolean') {
    type = (org === true)?'deep':org;
    org = args.shift();
    if (type == 'defaults') {
      org = aug({}, org); //clone defaults into new object
      type = 'strict';
    }
  }
  for (var i = 0, c = args.length; i < c; i++) {
    var prop = args[i];
    for (var name in prop) {
      if (type == 'deep' && typeof prop[name] === 'object' && typeof org[name] !== 'undefined') {
        aug(type, org[name], prop[name]);
      } else if (type != 'strict' || (type == 'strict' && typeof org[name] !== 'undefined')) {
        org[name] = prop[name];
      }
    }
  }
  return org;
};
if (typeof window === 'undefined') module.exports = aug;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
custom underscore extend
customExtend(options, addlOptions, {
  age: 27
});
ready
underscore extend
_.extend(options, addlOptions, {
  age: 27
});
ready
jquery deep extend
$.extend(true, options, addlOptions, {
  age: 27
});
ready
jquery extend
$.extend(options, addlOptions, {
  age: 27
});
ready
aug.js
aug(options, addlOptions, { age: 27 });
ready

Revisions

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