var firstObject = {
"name": "John Doe",
"age": 30,
"isStudent": false,
"address": {
"street": "123 Main St",
"city": "Anytown",
"zipcode": "12345"
},
"contacts": [
{
"type": "email",
"value": "john.doe@example.com"
},
{
"type": "phone",
"value": "+1 123-456-7890"
}
],
"skills": {
"programming": ["JavaScript", "Python", "Java"],
"design": ["Photoshop", "Illustrator"],
"languages": {
"spoken": ["English", "Spanish"],
"written": ["English", "French"]
}
},
"isActive": true,
"projects": [
{
"name": "Project A",
"status": "completed",
"team": ["Alice", "Bob"]
},
{
"name": "Project B",
"status": "in progress",
"team": ["Charlie", "David"]
}
]
}
const finalObject = {firstObject};
const finalObject = Object.assign(firstObject);
const finalObject = {}
Object.keys(firstObject).forEach(key => {finalObject[key] = firstObject[key] })
function deepClone(original) {
const cloned = {};
Object.keys(original).forEach(key => {
const value = original[key];
if (typeof value === 'object' && value !== null) {
// Recursively clone nested objects
cloned[key] = deepClone(value);
} else {
// Copy non-object values directly
cloned[key] = value;
}
});
return cloned;
}
const finalObject = deepClone(firstObject);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Using the spread operator | |
Using Object.assign | |
forEach | |
deep clone |
Test name | Executions per second |
---|---|
Using the spread operator | 19153344.0 Ops/sec |
Using Object.assign | 10854715.0 Ops/sec |
forEach | 1855195.9 Ops/sec |
deep clone | 377658.8 Ops/sec |
Measuring the performance of different methods for creating a deep clone of an object in JavaScript can be an interesting benchmark.
Overview
The benchmark compares four methods:
...
): This method creates a new object by spreading the properties of the original object.Object.assign()
: This method creates a new object and assigns properties from the original object to it.forEach()
with Object.keys()
: This method iterates over the properties of the original object using Object.keys()
and assigns each property value to a new object using the forEach()
method.deepClone()
): This is a custom function that recursively clones nested objects.Options comparison
Each method has its pros and cons:
Object.assign()
: Pros:forEach()
with Object.keys()
: Pros:deepClone()
): Pros:Library and custom functions
The deepClone()
function is a custom implementation that recursively clones nested objects. It uses Object.keys()
to iterate over the properties of the original object and assigns each property value to a new object using the forEach()
method.
Other considerations
deepClone()
function introduces additional complexity compared to the other methods.Alternatives
If you're interested in exploring alternative methods for creating deep clones, consider:
cloneDeep()
function: This function provides a more robust and flexible way to create deep clones, especially for large or complex objects._cloneDeep()
function: Similar to Lodash's cloneDeep()
, this function provides a simple and efficient way to create deep clones.Ultimately, the choice of method depends on your specific use case and performance requirements.