Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Cloning b81d8fae-190a-4c8b-a9af-08bebc52bf2a
(version: 0)
Comparing performance of:
DeepClone1 vs deepClone2 vs deepClone3 vs deepClone4 vs structuredClone vs JSON
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var deepClone1 = function(obj) { if (obj === null || typeof obj !== 'object') { return obj; } var clone = Object.assign({}, obj); Object.keys(clone).forEach(function(key) { clone[key] = deepClone1(clone[key]); }); return clone; } var deepClone2 = function(obj) { if (obj === null || typeof obj !== 'object') { return obj; } var clone = Object.assign({}, obj); for (var key in clone) { clone[key] = deepClone2(clone[key]); } return clone; } var isObject = function(obj) { return typeof obj === 'object' && obj !== null; } var deepClone3 = function(obj) { if (!isObject(obj)) { return obj; } var clone = Object.assign({}, obj); for (var key in clone) { clone[key] = deepClone3(clone[key]); } return clone; } var deepClone4 = function(obj) { if (obj === null || typeof obj !== 'object') { return obj; } let clone = {...obj} for (const key in clone) { clone[key] = deepClone4(clone[key]) } return clone } var testData = { name: 'John', age: 30, address: { street: '123 Main St', zip: 12345 }, hobbies: ['reading', 'swimming', 'hiking'], friends: [ { name: 'Jane', age: 25 }, { name: 'Bob', age: 32 }, { name: 'Alice', age: 27 } ], job: { salary: 100000, benefits: ['health insurance', '401k', 'stock options'] } };
Tests:
DeepClone1
const r = deepClone1(testData)
deepClone2
const r = deepClone2(testData)
deepClone3
const r = deepClone3(testData)
deepClone4
const r = deepClone4(testData)
structuredClone
const r = structuredClone(testData)
JSON
const r = JSON.parse(JSON.stringify(testData))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
DeepClone1
deepClone2
deepClone3
deepClone4
structuredClone
JSON
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript benchmark test case, where users can create and run microbenchmarks to compare the performance of different cloning functions. **Benchmark Definition** The benchmark definition is a JSON object that contains a list of individual test cases. Each test case has a unique name and a `Benchmark Definition` property that defines the specific cloning function to be tested. In this case, there are six test cases: 1. `DeepClone1`: Uses the `deepClone1` function from the provided script preparation code. 2. `deepClone2`: Uses the `deepClone2` function from the provided script preparation code. 3. `DeepClone3`: Uses the `deepClone3` function from the provided script preparation code. 4. `deepClone4`: Uses the `deepClone4` function from the provided script preparation code. 5. `structuredClone`: Uses the `structuredClone` function, which is a built-in JavaScript feature introduced in ECMAScript 2020. 6. `JSON`: Uses the `JSON.parse(JSON.stringify(testData))` approach, which relies on the `JSON` object and its `stringify` method. **Cloning Functions** Each cloning function is designed to recursively clone an object by creating a new copy of each property. The main difference between these functions lies in their implementation details: 1. **DeepClone1**: Uses `Object.assign()` to create a shallow copy of the object, and then recursively clones its properties using another instance of `deepClone1`. 2. **DeepClone2**: Uses `Object.assign()` to create a shallow copy of the object, and then recursively clones its properties using another instance of `deepClone2`. This function is similar to `DeepClone1`, but uses a different recursive cloning approach. 3. **DeepClone3**: Similar to `DeepClone1`, but uses `for...in` instead of `Object.keys()` to iterate over the object's properties. 4. **deepClone4**: Uses the spread operator (`{...obj}`) to create a new object with the same properties as the original, and then recursively clones its nested properties using another instance of `deepClone4`. 5. **structuredClone**: This is a built-in JavaScript function that creates a deep copy of an object by serializing it into a binary format and then deserializing it back into a new object. 6. **JSON**: Uses the `JSON.parse(JSON.stringify(testData))` approach, which relies on the `JSON` object's `stringify` method to serialize the object and its own `parse` method to deserialize it. **Performance Comparison** The benchmark results show that: * `structuredClone` is the fastest cloning function, with an execution speed of around 500-600 Hz. * `deepClone4` is the next fastest, followed by `DeepClone1`, `deepClone2`, and `DeepClone3`. These three functions have similar performance characteristics, but `deepClone4` is slightly faster due to its use of the spread operator. Overall, the choice of cloning function depends on the specific requirements of your application. If you need a fast and efficient way to clone objects, `structuredClone` or `deepClone4` might be good choices. However, if you're working with complex data structures that require deep recursion, one of the custom cloning functions (`DeepClone1`, `DeepClone2`, or `DeepClone3`) might be more suitable.
Related benchmarks:
withoutObjectKeys
withoutObjectKeys again
withoutObjectKeys again and again
Object.assign vs. JSON String/Parse vs deepclone
Comments
Confirm delete:
Do you really want to delete benchmark?