Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JSON.parse(JSON.stringify())1 vs structuredClone
(version: 0)
Comparing performance of:
JSON vs structuredClone
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function createBigObject(depth) { const obj = {}; for (let i = 0; i < 5000; i++) { obj[i + Math.floor(Math.random() * 10000)] = { [Math.random() * 10000]: Math.random() * 10000, }; } return obj; } var bigObj = createBigObject() var clone;
Tests:
JSON
clone = JSON.parse(JSON.stringify(bigObj))
structuredClone
clone = structuredClone(bigObj)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
JSON
structuredClone
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/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JSON
408.6 Ops/sec
structuredClone
454.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and considered. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches: `JSON.parse(JSON.stringify())` and `structuredClone()` on a large object cloning. The benchmark aims to determine which approach is faster and more efficient. **Test Cases** There are two test cases: 1. **JSON**: The first test case uses the classic way of cloning an object using `JSON.parse(JSON.stringify())`. This method creates a deep copy of the original object by recursively traversing its properties. 2. **structuredClone():** The second test case uses the `structuredClone()` function, which is introduced in ECMAScript 2020 as a new way to create a shallow or deep clone of an object. **Comparison** The benchmark compares the performance of these two approaches on a large object cloning: ```javascript const bigObj = createBigObject(); var clone; clone = JSON.parse(JSON.stringify(bigObj)); // Test Case: JSON ``` vs ```javascript structuredClone(bigObj); // Test Case: structuredClone ``` **Options Compared** The benchmark compares the following options: 1. **Deep Cloning**: `JSON.parse(JSON.stringify())` creates a deep copy of the original object, which can be slower and more memory-intensive. 2. **Shallow Cloning**: `structuredClone()` creates a shallow clone of the original object, which is faster but may not preserve all properties or values. **Pros and Cons** 1. **Deep Cloning (JSON.parse(JSON.stringify()))**: * Pros: Preserves all properties and values, can be useful for complex objects. * Cons: Slower, more memory-intensive due to the recursive traversal of properties. 2. **Shallow Cloning (structuredClone())**: * Pros: Faster, less memory-intensive since it doesn't traverse property values recursively. * Cons: May not preserve all properties or values. **Library and Purpose** The `structuredClone()` function is part of the ECMAScript specification and is designed to provide a more efficient way of cloning objects. Its primary purpose is to avoid the overhead of recursive traversal for shallow copies, while still preserving most properties and values. **Special JS Feature or Syntax** `structuredClone()` is a relatively new feature introduced in ECMAScript 2020, so it's not surprising that MeasureThat.net includes it as an option. This highlights the importance of keeping up-to-date with the latest JavaScript features and best practices. **Other Alternatives** If you need to clone objects frequently, you might want to consider other options: 1. **Lodash's `cloneDeep()`**: A popular utility library function that creates a deep copy of an object. 2. **Purestella's `createCopy()`**: Another utility library function that creates a shallow or deep copy of an object. Keep in mind that these alternatives might not be as optimized or efficient as the `structuredClone()` method, but they can provide a convenient solution when working with JavaScript objects.
Related benchmarks:
Lodash cloneDeep vs structuredClone vs JSON Parse (uint8array, 5MB)
Deep clone performance comparison
Lodash cloneDeep vs structuredClone vs JSON.parse + JSON.stringify but with big data
Lodash cloneDeep vs structuredClone vs JSON Parse (100 000 objects)
Comments
Confirm delete:
Do you really want to delete benchmark?