Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deep Clone vs JSON.Stringify
(version: 0)
Comparing performance of:
DeepClone vs JSON.stringify
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {}; var objCount = 100; for (let i = 0; i < objCount; i++) { obj[i] = [1, 2, 3]; } function deepClone(objIn) { let copy; if (objIn === null || typeof objIn !== 'object') { return obj; } if (objIn instanceof Array) { copy = []; for (let i = 0, len = objIn.length; i < len; i++) { copy[i] = deepClone(objIn[i]); } return copy; } if (objIn instanceof Object) { copy = {}; for (const attr in objIn) { if (objIn.hasOwnProperty(attr)) { copy[attr] = deepClone(objIn[attr]); } } return copy; } }
Tests:
DeepClone
var clone = deepClone(obj);
JSON.stringify
var clone = JSON.parse(JSON.stringify(obj));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
DeepClone
JSON.stringify
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
DeepClone
246233.0 Ops/sec
JSON.stringify
138912.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided JSON represents a JavaScript microbenchmark on MeasureThat.net, comparing the performance of two approaches: Deep Clone and JSON.stringify. The benchmark creates an object with 100 properties, each containing an array of three elements, and then clones this object using either the `deepClone` function or the `JSON.parse(JSON.stringify(obj))` method. **Deep Clone vs JSON.stringify** The `deepClone` function is a custom implementation that recursively copies the object's properties, ensuring that all nested objects and arrays are cloned correctly. The `JSON.stringify` method, on the other hand, uses the browser's native serialization algorithm to convert the object into a JSON string, which can then be parsed back into an object using the `JSON.parse()` method. **Options compared** The benchmark compares two options: 1. **Deep Clone**: A custom function that recursively clones the object's properties. 2. **JSON.stringify + JSON.parse**: Uses the browser's native serialization algorithm followed by parsing the resulting string back into an object. **Pros and Cons of each approach** * **Deep Clone**: + Pros: Can handle complex objects with nested arrays and objects, provides a controlled and predictable cloning process. + Cons: May be slower than JSON.stringify due to its recursive nature, and may not be optimized for performance. * **JSON.stringify + JSON.parse**: + Pros: Fast and efficient, as it leverages the browser's native serialization algorithm. + Cons: May not handle complex objects correctly (e.g., circular references), requires parsing the resulting string back into an object. **Library** In this benchmark, there is no external library used. The `deepClone` function is a custom implementation provided by the test author. **Special JavaScript feature or syntax** None are mentioned in this specific benchmark. However, it's worth noting that MeasureThat.net often uses special features like Web Workers, async/await, and Promises to create complex benchmarks. **Other alternatives** If you're interested in exploring alternative cloning methods, here are a few options: * **Lodash.cloneDeep**: A popular utility library function for deep cloning objects. * **JSON3.parse**: A fast and efficient way to parse JSON strings into objects. * **Object.assign**: Can be used to merge two objects, but may not provide the same level of control as `deepClone`. Keep in mind that each alternative has its own trade-offs and performance characteristics, so it's essential to evaluate them based on your specific use case.
Related benchmarks:
Lodash cloneDeep vs for loop vs JSON parse vs recursive clone deep vs recursive reduce clone deep
JSON.stringify vs Deep Clone
Deep Clone vs JSON.Stringify vs structuredClone
Object.assign vs. JSON String/Parse vs deepclone
Comments
Confirm delete:
Do you really want to delete benchmark?