extend vs shallow clone

JavaScript performance comparison

Test case created by kurkle

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function isArray(value) {
  		if (Array.isArray && Array.isArray(value)) {
  			return true;
  		}
  		var type = Object.prototype.toString.call(value);
  		if (type.substr(0, 7) === '[object' && type.substr(-6) === 'Array]') {
  			return true;
  		}
  		return false;
  	}
  
  function isObject(value) {
  		return value !== null && Object.prototype.toString.call(value) === '[object Object]';
  	}
  
  function each(loopable, fn, thisArg, reverse) {
  		var i, len, keys;
  		if (isArray(loopable)) {
  			len = loopable.length;
  			if (reverse) {
  				for (i = len - 1; i >= 0; i--) {
  					fn.call(thisArg, loopable[i], i);
  				}
  			} else {
  				for (i = 0; i < len; i++) {
  					fn.call(thisArg, loopable[i], i);
  				}
  			}
  		} else if (isObject(loopable)) {
  			keys = Object.keys(loopable);
  			len = keys.length;
  			for (i = 0; i < len; i++) {
  				fn.call(thisArg, loopable[keys[i]], keys[i]);
  			}
  		}
  	}
  
  function extend(target) {
  		var setFn = function(value, key) {
  			target[key] = value;
  		};
  		for (var i = 1, ilen = arguments.length; i < ilen; ++i) {
  			each(arguments[i], setFn);
  		}
  		return target;
  }
  
  var extend2 = Object.assign ? Object.assign : function(target) {
     var i, ilen, key;
  	 for (i = 1, ilen = arguments.length; i < ilen; ++i) {
        for(key in arguments[i]) {
          if(arguments[i].hasOwnProperty(key)) {
             target[key] = arguments[i][key];
          }
        }
     }
     return target;
  }
  
  var extend3 = Object.assign ? Object.assign : function(target) {
     var i, ilen, key, src;
  	 for (i = 1, ilen = arguments.length; i < ilen; ++i) {
        src = arguments[i];
        for(key in src) {
          if(src.hasOwnProperty(key)) {
            target[key] = src[key];
          }
        }
     }
     return target;
  }
  
  var extend4 = Object.assign ? Object.assign : function(target) {
     var i, ilen, obj, keys, src, j;
  	 for (i = 1, ilen = arguments.length; i < ilen; ++i) {
        obj = arguments[i];
        keys = Object.keys(obj);
        j = keys.length;
        while(j--) {
          target[keys[j]] = obj[keys[j]];
        }
     }
     return target;
  }
  var extend5 = Object.assign ? Object.assign : function(target) {
     var i, ilen, obj, keys, src, j;
  	 for (i = 1, ilen = arguments.length; i < ilen; ++i) {
        obj = arguments[i];
        keys = Object.keys(obj);
        for(j = keys.length - 1; j >= 0; --j) {
          target[keys[j]] = obj[keys[j]];
        }
     }
     return target;
  }
  var extend6 = Object.assign ? Object.assign : function(target) {
     var i, obj, keys, j;
  	 for (i = 1; i < arguments.length; ++i) {
        obj = arguments[i];
        keys = Object.keys(obj);
        j = keys.length;
        while(j--) {
          target[keys[j]] = obj[keys[j]];
        }
     }
     return target;
  }
  var obj = {
      prop1: "abc",
      prop2: "def",
      prop3: "ghi",
      prop4: "jkl",
      prop5: "mno",
      prop6: "pqr",
      prop7: "stu",
      prop8: "vwx",
      prop9: "yz",
      prop10: "abc",
      prop11: "abc",
      prop12: "def",
      prop13: "ghi",
      prop14: "jkl",
      prop15: "mno",
      prop16: "pqr",
      prop17: "stu",
      prop18: "vwx",
      prop19: "yz",
      prop20: "yz",
      prop21: "abc",
      prop22: "def",
      prop23: "ghi",
      prop24: "jkl",
      prop25: "mno",
      prop26: "pqr",
      prop27: "stu",
      prop28: "vwx",
      prop29: "yz",
      prop31: "abc",
      prop32: "def",
      prop33: "ghi",
      prop34: "jkl",
      prop35: "mno",
      prop36: "pqr",
      prop37: "stu",
      prop38: "vwx",
      prop39: "yz",
      prop41: "abc",
      prop42: "def",
      prop43: "ghi",
      prop44: "jkl",
      prop45: "mno",
      prop46: "pqr",
      prop47: "stu",
      prop48: "vwx",
      prop49: "yz",
      prop51: "abc",
      prop52: "def",
      prop53: "ghi",
      prop54: "jkl",
      prop55: "mno",
      prop56: "pqr",
      prop57: "stu",
      prop58: "vwx",
      prop59: "yz",
      prop61: "abc",
      prop62: "def",
      prop63: "ghi",
      prop64: "jkl",
      prop65: "mno",
      prop66: "pqr",
      prop67: "stu",
      prop68: "vwx",
      prop69: "yz"
  }
  
  obj.prototype = { styff: 'asdf', more: 'asdasddasd' };

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
extend
extend({}, obj);
pending…
extend2
extend2({}, obj);
pending…
extend3
extend3({}, obj);
pending…
extend4
extend4({}, obj);
pending…
extend5
extend5({}, obj);
pending…
extend6
extend6({}, obj);
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