Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deep copy : JSON vs Specified-Method
(version: 0)
Comparing performance of:
JSON vs Copy (no generic) vs Copy (generic)
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function copyX(x) { return { a: x.a, b: x.b, c: { a: x.c.a } } } function cloneObject(obj) { var clone = {}; for(var i in obj) { if(obj[i] != null && typeof(obj[i])=="object") clone[i] = cloneObject(obj[i]); else clone[i] = obj[i]; } return clone; } var x = { a: 444, b: 666, c: { a: "dddd" } }
Tests:
JSON
JSON.parse(JSON.stringify(x))
Copy (no generic)
copyX(x)
Copy (generic)
cloneObject(x)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
JSON
Copy (no generic)
Copy (generic)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JSON
1529252.0 Ops/sec
Copy (no generic)
6160773.5 Ops/sec
Copy (generic)
2802696.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and its test cases. **Benchmark Overview** The benchmark measures how fast three approaches are to create a deep copy of an object: using JSON, using a custom `copyX` function, and using a generic cloning function (`cloneObject`). **Approaches Compared** 1. **JSON**: Using `JSON.parse(JSON.stringify(x))`, where `x` is the original object. 2. **Custom `copyX`**: A simple, non-generic function that manually copies the properties of `x`. 3. **Generic `cloneObject`**: A function that recursively clones an object's properties. **Pros and Cons** 1. **JSON**: Pros: * Easy to implement * Fast, as it only involves serializing and deserializing the object Cons: * May not work correctly with complex objects or cyclic references * Can be slower than custom implementations for large objects 2. **Custom `copyX`**: Pros: * Control over the copying process * May be faster than JSON for large objects Cons: * Requires manual implementation, which can lead to errors * May not handle complex or cyclic references correctly 3. **Generic `cloneObject`**: Pros: * Handles complex and cyclic references correctly * Less prone to errors compared to custom implementations Cons: * Can be slower than JSON for very large objects due to recursive cloning **Library Usage** The benchmark uses the `JSON.parse()` and `JSON.stringify()` methods, which are part of the JavaScript Standard Library. These methods provide a simple way to serialize and deserialize objects. **Special JS Feature or Syntax** None mentioned explicitly in this benchmark. However, it's worth noting that the `let` and `var` declarations used in the script preparation code are not strictly necessary and can be omitted if you're targeting modern browsers or environments that support ES6 syntax. **Other Alternatives** If you want to explore other approaches, consider these: * Using a library like Lodash or Underscore.js, which provide functions for deep cloning objects. * Implementing a custom cloning function using a different algorithm, such as iterating over the object's properties and recursively cloning each value. * Comparing performance with other methods, such as using `Array.prototype.slice()` to create a shallow copy of an array-like object or using `Object.assign()` to create a shallow copy of an object. These alternatives can provide more insight into optimal cloning strategies, but may require additional code and experimentation.
Related benchmarks:
Lodash cloneDeep vs for loop vs JSON parse vs recursive clone deep vs recursive reduce clone deep
Comparing deep cloning methods (small object): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (array of objects): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (small object): Lodash <> Custom clone func <> JSON.parse <> structuredClone 2
Comments
Confirm delete:
Do you really want to delete benchmark?