<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
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...',
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...',
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...',
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...',
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...',
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...',
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...',
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...'
}
}
}
}
}
}
}
}
};
var myCopy = null;
myCopy = _.cloneDeep(MyObject);
myCopy = structuredClone(MyObject);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
Native structuredClone |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 224228.7 Ops/sec |
Native structuredClone | 113827.8 Ops/sec |
Let's break down the provided JSON and explain what's being tested, compared, and analyzed.
Benchmark Overview
The benchmark measures the performance of two approaches to create a deep copy of an object: _.cloneDeep
from Lodash and structuredClone
. The object being copied is a complex nested structure with various types of values (numbers, booleans, objects).
Library: Lodash
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as data manipulation, string manipulation, and more. In this case, _.cloneDeep
is used to create a deep copy of an object.
Library: structuredClone
structuredClone
is a new API introduced in modern browsers (starting from Chrome 67) that provides a way to create a deep copy of an object without using the JSON.parse()
and JSON.stringify()
methods. This API aims to improve performance and security compared to traditional JSON-based cloning.
Comparison Options
The benchmark compares two approaches:
_.cloneDeep
function from Lodash to create a deep copy of the object.structuredClone
API to create a deep copy of the object in modern browsers.Pros and Cons
Lodash cloneDeep:
Pros:
structuredClone
)Cons:
structuredClone
in modern browsersNative structuredClone:
Pros:
JSON.parse()
and JSON.stringify()
Cons:
Other Considerations
The benchmark also considers the following factors:
Alternatives
Other alternatives for creating deep copies of objects include:
Array.prototype.slice()
: Creates a shallow copy of an arrayObject.assign()
: Creates a shallow copy of an objectlodash
's _.clone()
)However, these alternatives may not provide the same level of performance and security as structuredClone
in modern browsers.