Test name | Executions per second |
---|---|
Using the spread operator | 19634364.0 Ops/sec |
Using Object.assign | 11138351.0 Ops/sec |
forEach | 1897219.0 Ops/sec |
deep clone | 384517.7 Ops/sec |
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);