Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Lodash cloneDeep vs structuredClone 2222
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser:
Chrome 135
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
Lodash cloneDeep
249228.8 Ops/sec
Native structuredClone
179351.8 Ops/sec
DeepCopy
749346.0 Ops/sec
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var MyObject2 = { 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 MyObject = { "userId": "134374683951759360", "guild": "86908735326281728", "status": "online", "activities": [{ "name": "Twitch", "type": 1, "url": "https://www.twitch.tv/craggle25", "details": "Weekend dirt naps with @mart0k and Cat. Hunt Showdown", "state": "Hunt: Showdown", "applicationId": null, "timestamps": null, "party": null, "assets": { "largeText": null, "smallText": null, "largeImage": "twitch:craggle25", "smallImage": null }, "flags": 0, "emoji": null, "buttons": [], "createdTimestamp": 1697880668811 }], "clientStatus": { "desktop": "online" } }; var myCopy = null; function deepCopy(data) { let node; if (Array.isArray(data)) { node = data.length > 0 ? data.slice(0) : []; node.forEach((e, i) => { if (typeof e === 'object' || (Array.isArray(e) && e.length > 0)) { node[i] = deepCopy(e); } }); } else if (data && typeof data === 'object') { node = data instanceof Date ? data : Object.assign({}, data); Object.keys(node).forEach(key => { if (typeof node[key] === 'object' || (Array.isArray(node[key]) && node[key].length > 0)) { node[key] = deepCopy(node[key]); } }); } else { node = data; } return node; // structuredClone(data) } function isArr(x) { return Array.isArray(x); } function isObject(x) { return typeof x === 'object'; } function shallowClone(obj) { let clone = {}; for (let key in obj) { let r = obj[key]; let isArray = isArr(r); if (!isArray && isObject(r)) { continue; } if (isArray) { clone[key] = []; for (let i = 0; i < r.length; i++) { clone[key][i] = shallowClone(r[i]); } } else { clone[key] = r; } } return clone; } // pulled this from https://github.com/nodejs/node/issues/34355#issuecomment-658394617 function deepClone(o) { if (typeof o !== "object") { return o } if (!o) { return o } // https://jsperf.com/deep-copy-vs-json-stringify-json-parse/25 if (Array.isArray(o)) { const newO = [] for (let i = 0; i < o.length; i += 1) { const val = !o[i] || typeof o[i] !== "object" ? o[i] : deepClone(o[i]) newO[i] = val === undefined ? null : val } return newO } const newO = {} for (const i of Object.keys(o)) { const val = !o[i] || typeof o[i] !== "object" ? o[i] : deepClone(o[i]) if (val === undefined) { continue } newO[i] = val } return newO } let iterations = [100, 10000, 1000000, 10000000]; function profile(name, func) { for (let iterationCount of iterations) { let n = name + " at " + iterationCount + " iterations"; console.time(n); for (let i = 0; i < iterationCount; i++) { func(); } console.timeEnd(n); } console.log(""); } profile('StructuredClone', () => { structuredClone(MyObject); }); profile('ParseStringify', () => { JSON.parse(JSON.stringify(MyObject)) }); profile('ShallowClone', () => { shallowClone(MyObject); }); profile('DeepClone', () => { deepClone(MyObject); }); profile('DeepCopy', () => { deepCopy(MyObject); }); profile('lodash', () => { _.cloneDeep(MyObject); });
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
Native structuredClone
myCopy = structuredClone(MyObject);
DeepCopy
myCopy = deepCopy(MyObject);