Lodash cloneDeep vs structuredClone vs cloneObject (custom func)
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Date tested:
one year ago
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
Test name
Executions per second
Lodash cloneDeep
571579.1 Ops/sec
Native structuredClone
241363.4 Ops/sec
My shitty simple clone logic
1168613.0 Ops/sec
Benchmark definition (click to collapse):
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var MyObject = { description: 'Creates a deep copy of source, which should be an object or an array.', myNumber: 123456789, myBoolean: true, jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' } }; var myCopy = null; function cloneObject(o) { if (typeof o !== 'object' || o === null) return o; let out = Array.isArray(o) ? [] : {}; for (let k in o) { out[k] = (typeof o[k] === "object" && o[k] !== null) ? cloneObject(o[k]) : o[k]; } return out; }
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
Native structuredClone
myCopy = structuredClone(MyObject);
My shitty simple clone logic
myCopy = cloneObject(MyObject);
Open this result on MeasureThat.net