Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deep clone JS Object
(version: 0)
Deep clone JS Object
Comparing performance of:
structuredClone vs JSON
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
structuredClone
const anObj = { k1: "hello", k2: { k3: "world", k4: [ { k5: "I", k6: "doing", k7: [[true, null]] } ] }, k8: [2023, { k9: "what" }] } const newObj = structuredClone(anObj)
JSON
const anObj = { k1: "hello", k2: { k3: "world", k4: [ { k5: "I", k6: "doing", k7: [[true, null]] } ] }, k8: [2023, { k9: "what" }] } const newObj = JSON.parse(JSON.stringify(anObj))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
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):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to measure the performance of two different methods for deep cloning JavaScript objects: `JSON` and `structuredClone`. Deep cloning means creating a new, independent copy of an object that includes all its properties and nested structures. **Options Being Compared** There are two options being compared: 1. **JSON**: This method uses the `JSON.parse(JSON.stringify(anObj))` syntax to create a deep clone of the input object `anObj`. The idea is to parse the JSON representation of `anObj`, then use the `JSON.parse()` function again with the original object as the input. 2. **structuredClone**: This method uses the `structuredClone()` function, which was introduced in ECMAScript 2020 (ES10) as a built-in function for creating deep clones of objects. **Pros and Cons** * **JSON**: While simple to use, this method can be slow due to the overhead of parsing JSON multiple times. It also doesn't handle certain edge cases, such as arrays with `undefined` values or circular references. * **structuredClone**: This method is specifically designed for deep cloning objects and is more efficient than the `JSON` approach. However, it may not work in older browsers that don't support this function. **Other Considerations** * The benchmark uses a simple JavaScript object as input, with one level of nesting. In real-world scenarios, you might need to handle more complex objects with multiple levels of nesting. * The benchmark doesn't consider issues like memory allocation or garbage collection, which can affect performance in certain situations. * The `structuredClone()` function is a relatively new addition to JavaScript, and its adoption may not be uniform across all browsers. **Library or Special JS Feature Used** The benchmark uses the `JSON.parse(JSON.stringify(anObj))` syntax, which relies on the `JSON.stringify()` method. This method has been available in JavaScript since ES5. **Special JS Feature or Syntax (if applicable)** There is no special JavaScript feature or syntax used in this benchmark. The focus is solely on measuring the performance of two different cloning methods. **Alternatives** If you need to clone a JavaScript object, other alternatives include: 1. **Lodash.cloneDeep()**: A popular utility library that provides a `cloneDeep()` function for deep cloning objects. 2. **Underscore.js.clone()**: Another utility library that provides a `clone()` function for deep cloning objects. 3. **Manual implementation**: You can implement your own deep cloning algorithm using recursion or iteration, which might be more efficient than the built-in methods. Keep in mind that the performance of these alternatives may vary depending on the specific use case and browser support.
Related benchmarks:
Object Deep Copy with deep clone
Comparing deep cloning methods (small object): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (Complex object): Lodash <> Custom clone func <> JSON.parse <> structuredClone
is lodash cloneDeep the BEST object deep cloner ? what about native structuredClone function ?
Cloning b81d8fae-190a-4c8b-a9af-08bebc52bf2a
Comments
Confirm delete:
Do you really want to delete benchmark?