Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JSON clone vs structuredClone
(version: 0)
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Comparing performance of:
JSON clone vs Native structuredClone
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var MyObject = { description: 'Creates a deep copy of source, which should be an object or an array.', myNumber: 123456789, myBoolean: true, jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' } }; var myCopy = null;
Tests:
JSON clone
myCopy = JSON.parse(JSON.stringify(MyObject));
Native structuredClone
myCopy = structuredClone(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
JSON clone
Native structuredClone
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 dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided benchmark compares two approaches to create a copy of an object: `JSON.parse(JSON.stringify(MyObject))` and `structuredClone(MyObject)`. The test uses a JavaScript object `MyObject` with nested properties, including a `jayson` property that references other JavaScript objects and functions. **Options Compared** There are two options compared in this benchmark: 1. **JSON Clone**: `myCopy = JSON.parse(JSON.stringify(MyObject));` 2. **Native structuredClone**: `myCopy = structuredClone(MyObject);` **Pros and Cons of Each Approach** ### JSON Clone * **Pros**: + Widely supported by most browsers, including older versions. + Simple to implement, as it uses the built-in `JSON` object and string manipulation functions. * **Cons**: + Can lead to issues with cyclic references, where an object points back to itself, causing infinite recursion during cloning. + May not preserve all properties or nuances of the original object, especially if they involve functions, callbacks, or other complex data types. ### Native structuredClone * **Pros**: + Designed specifically for creating deep copies of objects and arrays, with built-in support for cyclic references and property preservation. + More efficient than JSON clone, as it uses a more optimized algorithm to create the copy. * **Cons**: + Requires modern JavaScript versions (ECMAScript 2017 or later) and specific browser implementations that support `structuredClone`. + May not be widely supported by older browsers. **Library Used** In this benchmark, the `lodash` library is used in the HTML preparation code. However, it's not directly related to the benchmark itself, as the test uses built-in JavaScript functions (`JSON.parse`, `structuredClone`) instead of any custom libraries provided by `lodash`. **Special JS Features or Syntax** There are no special JavaScript features or syntaxes mentioned in this benchmark that would require additional explanation. **Other Alternatives** If you need to create copies of objects or arrays in your own code, other approaches might be considered: * **Recursion**: Manually implementing a recursive function to clone the object, which can lead to stack overflows for large inputs. * **Serialization and Deserialization**: Converting the object to a string (e.g., using `JSON.stringify`) and then parsing it back into an object, which can result in issues with cyclic references or data type changes. * **Using a Library**: Depending on your specific requirements, you might consider using dedicated libraries like Lodash's `cloneDeep` function, which provides more robust copy creation than the `structuredClone` method. Keep in mind that these alternatives are not as efficient or reliable as the native `structuredClone` approach and may require more development effort to implement correctly.
Related benchmarks:
Lodash cloneDeep vs structuredClone vs JSON clone s
Lodash cloneDeep vs structuredClone vs JSON.stringify (small object)
Lodash cloneDeep vs structuredClone vs JSON Parse (deep object)
Lodash cloneDeep vs structuredClone vs JSON-Clone
Lodash cloneDeep vs structuredClone vs JSON-JSON
Comments
Confirm delete:
Do you really want to delete benchmark?