var myObject = {
name: 'Jane Darlene',
age: 25,
score: 120,
weight: 65.5,
balabnce: 2000.0,
premium: false,
birthDate: '2024-10-02T03:23:58.663Z',
testNull: null,
title: 'Some other title',
description: 'Some other description',
address: {
street: '123 Main St',
city: 'Anytown',
state: 'NY',
zip: 12345,
country: 'USA',
},
additionalAddresses: [{
street: '456 Elm St',
city: 'Anytown',
state: 'NY',
zip: 12345,
country: 'USA',
},
{
street: '789 Oak St',
city: 'Anytown',
state: 'NY',
zip: 12345,
country: 'USA',
},
],
profile: {
firstName: 'Jane',
lastName: 'Darlene',
},
features: ['One', 'Two', 'Three'],
favorites: [{
slug: 'first',
postname: 'First post'
},
{
slug: 'second',
postname: 'Second post'
},
],
todos: {
1: {
id: 1,
text: 'First todo',
done: false
},
2: {
id: 2,
text: 'Second todo',
done: true
},
},
};
structuredClone(myObject);
JSON.parse(JSON.stringify(myObject));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
structuredClone(myObject) | |
JSON.parse(JSON.stringify(myObject)) |
Test name | Executions per second |
---|---|
structuredClone(myObject) | 33920.0 Ops/sec |
JSON.parse(JSON.stringify(myObject)) | 45901.5 Ops/sec |
Let's break down the benchmark and explain what's being tested, the options compared, their pros and cons, and other considerations.
What's being tested:
The benchmark is comparing two ways to create a deep copy of a JavaScript object:
structuredClone(myObject)
JSON.parse(JSON.stringify(myObject))
Both methods are used to create a new, independent copy of the original object, but they differ in their implementation and potential performance.
Options compared:
The benchmark is comparing two options for creating a deep copy of a JavaScript object:
A) structuredClone(myObject)
B) JSON.parse(JSON.stringify(myObject))
Pros and Cons:
structuredClone(myObject)
JSON.parse(JSON.stringify(myObject))
due to its more complex implementation.JSON.parse(JSON.stringify(myObject))
structuredClone(myObject)
due to its simpler implementation.Other considerations:
structuredClone(myObject)
might be a better choice due to its designed efficiency and safety features.structuredClone(myObject)
and JSON.parse(JSON.stringify(myObject))
might not be noticeable.Library usage:
In this benchmark, structuredClone()
is used as a built-in JavaScript function, which means it's part of the ECMAScript standard. On the other hand, JSON.stringify()
is also a built-in JavaScript function, but its purpose is to serialize objects into a JSON string, rather than create a deep copy.
Special JS features or syntax:
None mentioned in this benchmark.
Alternatives:
If you need a more efficient or safer way to create deep copies of objects, you can consider using other libraries or polyfills, such as:
cloneDeep()
deepClone()
json-stable-stringify
Keep in mind that these alternatives might introduce additional dependencies and may not be supported by older browsers.
Overall, the benchmark is testing two widely used methods for creating deep copies of JavaScript objects: structuredClone()
and JSON.parse(JSON.stringify(myObject))
. By comparing their performance and characteristics, developers can make informed decisions about which method to use in their own projects.