<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...',
a: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...',
a: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...',
a: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...',
a: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...',
a: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...',
a: {
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 | 535768.9 Ops/sec |
Native structuredClone | 242265.7 Ops/sec |
Let's dive into the details of the benchmark.
Benchmark Overview
The test is designed to compare two methods for creating a deep copy of an object: _.cloneDeep
from the Lodash library and structuredClone
. The test case uses a predefined JavaScript object (MyObject
) with nested properties, including arrays and objects with complex structures.
Library and Purpose
Lodash's cloneDeep
method is used to create a deep copy of an object. It recursively creates new objects for all properties of the original object, ensuring that the resulting copy is independent from the original.
The native structuredClone
function (introduced in Chrome 123) is also used as a test case. It's a new API designed to create immutable clones of JavaScript values, including objects. Its purpose is to provide a safer alternative to traditional cloning methods like JSON.stringify() or Object.assign()
.
Comparison Options
In this benchmark, two options are compared:
structuredClone
function to create an immutable clone of the object.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Special JS Features or Syntax
In this benchmark, the structuredClone
function is used, which introduces a new feature in JavaScript. This function allows for creating immutable clones of JavaScript values and is designed to be more efficient and safe than traditional cloning methods. The syntax is simple and straightforward: structuredClone(obj)
.
Other Alternatives
If you're interested in exploring alternative deep copy methods, here are some other options:
Keep in mind that each of these alternatives has its pros and cons, and some may be more suitable for specific use cases or performance requirements.
I hope this explanation helps!