Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
JSON.stringify vs structuredClone vs simple deepCopyObject 3
JSON.stringify vs structuredClone vs simple deepCopyObject 3
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser:
Chrome 126
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
JSON.stringify
1446061.1 Ops/sec
structuredClone
817268.3 Ops/sec
deepCopyObject
1303167.8 Ops/sec
deepCopyObjectSimple
3451855.5 Ops/sec
deepCopyObjectIterative
3508498.8 Ops/sec
Script Preparation code:
var deepCopyObject = (inObject) => { if (inObject instanceof Date) return inObject if (inObject instanceof Set) return new Set(inObject) if (inObject instanceof Map) return new Map(inObject) if (typeof inObject !== 'object' || inObject === null) { return inObject // Return the value if inObject is not an object } // Create an array or object to hold the values const outObject = Array.isArray(inObject) ? [] : {} Object.keys(inObject).forEach((key) => { const value = inObject[key] // Recursively (deep) copy for nested objects, including arrays outObject[key] = typeof value === 'object' && value !== null ? deepCopyObject(value) : value }) return outObject } function deepCopyObjectSimple(inObject) { if (typeof inObject !== 'object' || inObject === null) { return inObject; } const outObject = Array.isArray(inObject) ? [] : {}; for (const key in inObject) { const value = inObject[key]; outObject[key] = (typeof value === 'object' && value !== null) ? deepCopyObjectSimple(value) : value; } return outObject; } function deepCopyObjectIterative(inObject) { if (typeof inObject !== 'object' || inObject === null) { return inObject; } const stack = [{ source: inObject, target: Array.isArray(inObject) ? [] : {} }]; const root = stack[0].target; while (stack.length > 0) { const { source, target } = stack.pop(); for (const key in source) { const value = source[key]; if (typeof value === 'object' && value !== null) { target[key] = Array.isArray(value) ? [] : {}; stack.push({ source: value, target: target[key] }); } else { target[key] = value; } } } return root; } 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;
Tests:
JSON.stringify
myCopy = JSON.parse(JSON.stringify(MyObject));
structuredClone
myCopy = structuredClone(MyObject);
deepCopyObject
myCopy = deepCopyObject(MyObject);
deepCopyObjectSimple
myCopy = deepCopyObjectSimple(MyObject);
deepCopyObjectIterative
myCopy = deepCopyObjectIterative(MyObject);