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...'
},
deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
};
var myCopy = null;
myCopy = JSON.parse(JSON.stringify(MyObject));
myCopy = structuredClone(MyObject);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
JSON.stringify | |
structuredClone |
Test name | Executions per second |
---|---|
JSON.stringify | 195723.2 Ops/sec |
structuredClone | 276192.2 Ops/sec |
Overview of the Benchmark
The provided benchmark compares two JavaScript methods for creating copies of objects: JSON.stringify
and structuredClone
. The benchmark measures which method is faster and more efficient.
Options Compared
Two options are compared:
JSON.parse()
.JSON.stringify
for large objects.Pros and Cons
JSON.stringify
, may require additional dependencies or libraries.Library Used
Special JavaScript Feature/Syntax
structuredClone
is designed to handle these cases efficiently.structuredClone
does so without converting it to a JSON string.Other Alternatives
If you need to compare other serialization techniques, here are some alternatives:
structuredClone
, this method can still be used for simple object copying.In summary, the benchmark compares two popular methods for serializing JavaScript objects: JSON.stringify
and structuredClone
. While JSON.stringify
is widely supported and simple to use, it has limitations when dealing with cyclic references or large objects. structuredClone
provides a more efficient solution but is less widely adopted.