cloning objects strategies

Benchmark created by Gonzalo Beviglia on


Preparation HTML

<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>

Setup

const _extends = function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  
  const spread = function spread(o) {
    return _extends({}, o);
  };
  
  const objectArray = _.times(100, (i) => ({ id: i, name: `Object ${i}` }));

Test runner

Ready to run.

Testing in
TestOps/sec
lodash clone
objectArray.map(o => _.clone(o));
ready
lodash clone deep
objectArray.map(o => _.cloneDeep(o));
ready
Object.assign
objectArray.map(o => Object.assign({}, o));
ready
Assign polyfill
objectArray.map(o => spread(o));
ready

Revisions

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

  • Revision 1: published by Gonzalo Beviglia on