Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deep copy comparison gil
(version: 0)
Benchmarks for StackOverflow question on deep copy techniques https://stackoverflow.com/questions/7486085/copy-array-by-value/23536726#23536726
Comparing performance of:
spread ... vs JSON.parse(JSON.stringify()) vs structuredClone()
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
spread ...
const arr1 = {"in":1,"inT":1716588594541,"keep":1,"kind":4,"lastEvtT":1716588594541,"name":"gil","oTips":{"amt":1,"stta":1,"count":1,"lastT":1716588594541,"amtEver":1},"outT":1716588594541,"sex":"m","spanT":0}; const arr2 = { ...arr1 };
JSON.parse(JSON.stringify())
const arr1 = {"in":1,"inT":1716588594541,"keep":1,"kind":4,"lastEvtT":1716588594541,"name":"gil","oTips":{"amt":1,"stta":1,"count":1,"lastT":1716588594541,"amtEver":1},"outT":1716588594541,"sex":"m","spanT":0}; const arr2 = JSON.parse(JSON.stringify(arr1));
structuredClone()
const arr1 = {"in":1,"inT":1716588594541,"keep":1,"kind":4,"lastEvtT":1716588594541,"name":"gil","oTips":{"amt":1,"stta":1,"count":1,"lastT":1716588594541,"amtEver":1},"outT":1716588594541,"sex":"m","spanT":0}; const arr2 = structuredClone(arr1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
spread ...
JSON.parse(JSON.stringify())
structuredClone()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread ...
33551980.0 Ops/sec
JSON.parse(JSON.stringify())
809628.0 Ops/sec
structuredClone()
613758.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definitions and explain what's being tested, the options compared, their pros and cons, and other considerations. **Benchmark Context** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The website provides a way to test various aspects of JavaScript performance, including object creation, comparison, and cloning methods. **Benchmark Definition JSON** The provided benchmark definition JSON contains three test cases: 1. "spread ..." 2. "JSON.parse(JSON.stringify())" 3. "structuredClone()" Each test case represents a different approach for deep copying an object in JavaScript. **Test Case 1: Spread Operator** ```javascript const arr1 = {\"in\":1,\"inT\":1716588594541,\"keep\":1,\"kind\":4,\"lastEvtT\":1716588594541,\"name\":\"gil\",\"oTips\":{\"amt\":1,\"stta\":1,\"count\":1,\"lastT\":1716588594541,\"amtEver\":1},\"outT\":1716588594541,\"sex\":\"m\",\"spanT\":0};\r\nconst arr2 = { ...arr1 }; ``` **Test Case 2: JSON.parse(JSON.stringify())** ```javascript const arr1 = {\"in\":1,\"inT\":1716588594541,\"keep\":1,\"kind\":4,\"lastEvtT\":1716588594541,\"name\":\"gil\",\"oTips\":{\"amt\":1,\"stta\":1,\"count\":1,\"lastT\":1716588594541,\"amtEver\":1},\"outT\":1716588594541,\"sex\":\"m\",\"spanT\":0};\r\nconst arr2 = JSON.parse(JSON.stringify(arr1)); ``` **Test Case 3: structuredClone()** ```javascript const arr1 = {\"in\":1,\"inT\":1716588594541,\"keep\":1,\"kind\":4,\"lastEvtT\":1716588594541,\"name\":\"gil\",\"oTips\":{\"amt\":1,\"stta\":1,\"count\":1,\"lastT\":1716588594541,\"amtEver\":1},\"outT\":1716588594541,\"sex\":\"m\",\"spanT\":0};\r\nconst arr2 = structuredClone(arr1); ``` **Options Compared** * `Spread Operator` (`{ ...arr1 }`) * `JSON.parse(JSON.stringify())` * `structuredClone()` (new in ECMAScript 2022) **Pros and Cons:** * **Spread Operator**: + Pros: Simple, efficient, and widely supported. + Cons: Not all objects are cloned correctly (e.g., functions, dates). * **JSON.parse(JSON.stringify())**: + Pros: Works for most object types, including arrays and JSON objects. + Cons: Can be slow for large objects, as it involves parsing and re-parsing the JSON representation. Additionally, this method can be vulnerable to certain attacks (e.g., exploiting the `Object.prototype`). * **structuredClone()**: + Pros: New in ECMAScript 2022, designed specifically for deep cloning objects. + Cons: Still a relatively new feature, and not all browsers support it. Additionally, its performance may vary depending on the specific use case. **Other Considerations** * The test cases focus on the performance of each cloning method, rather than their correctness or semantic implications. * The benchmark results are provided for Chrome 125, running on a Mac OS X 10.15.7 machine. **Alternatives** For those interested in exploring alternative methods for deep copying objects in JavaScript: * **Lodash's `cloneDeep()` function**: A popular library providing a robust and efficient cloning mechanism. * **Underscore.js's `_clone()` function**: Another well-known library offering a convenient cloning solution. * **Manual object creation using constructors or `Object.assign()```
Related benchmarks:
Lodash cloneDeep vs for loop vs JSON parse vs recursive clone deep vs recursive reduce clone deep
Object Deep Copy with
Array shallow copy - slice(0) vs conditional for() loop
lodash cloneDeep vs. JSON.parse(JSON.stringify()) vs. fastest-json-copy vs. makePlain | On a Small Object
Deep Clone vs JSON.Stringify vs structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?