<script src="https://cdn.jsdelivr.net/npm/rfdc@1.1.4/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>
var sampleObject = [{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters": {
"batter": [{
"id": "1001",
"type": "Regular"
},
{
"id": "1002",
"type": "Chocolate"
},
{
"id": "1003",
"type": "Blueberry"
},
{
"id": "1004",
"type": "Devil's Food"
}
]
},
"topping": [{
"id": "5001",
"type": "None"
},
{
"id": "5002",
"type": "Glazed"
},
{
"id": "5005",
"type": "Sugar"
},
{
"id": "5007",
"type": "Powdered Sugar"
},
{
"id": "5006",
"type": "Chocolate with Sprinkles"
},
{
"id": "5003",
"type": "Chocolate"
},
{
"id": "5004",
"type": "Maple"
}
]
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters": {
"batter": [{
"id": "1001",
"type": "Regular"
}]
},
"topping": [{
"id": "5001",
"type": "None"
},
{
"id": "5002",
"type": "Glazed"
},
{
"id": "5005",
"type": "Sugar"
},
{
"id": "5003",
"type": "Chocolate"
},
{
"id": "5004",
"type": "Maple"
}
]
},
{
"id": "0003",
"type": "donut",
"name": "Old Fashioned",
"ppu": 0.55,
"batters": {
"batter": [{
"id": "1001",
"type": "Regular"
},
{
"id": "1002",
"type": "Chocolate"
}
]
},
"topping": [{
"id": "5001",
"type": "None"
},
{
"id": "5002",
"type": "Glazed"
},
{
"id": "5003",
"type": "Chocolate"
},
{
"id": "5004",
"type": "Maple"
}
]
}
];
var sampleString = JSON.stringify(sampleObject)
var myCopy = null;
myCopy = _.cloneDeep(sampleObject);
myCopy = JSON.parse(sampleString);
var clone = rfdc();
myCopy = clone(sampleObject);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
Json clone | |
RFDC copy |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 24560.5 Ops/sec |
Json clone | 74752.7 Ops/sec |
RFDC copy | 86970.2 Ops/sec |
I'll provide an explanation of the benchmark, its options, pros and cons, and other considerations.
Benchmark Overview
The benchmark compares the performance of three different methods for cloning or copying a JavaScript object:
cloneDeep
functionJSON.parse
method (which essentially creates a shallow copy by parsing the original JSON string)rfdc
) which clones the entire object graph.Option Comparison
_.cloneDeep
)Pros:
Cons:
JSON.parse
(JSON.clone
)Pros:
Cons:
rfdc
)Pros:
JSON.parse
Cons:
Other Considerations
JSON.parse
method assumes that the original object is a valid JSON string. If the input data is not well-formed, this approach will throw an error.Library Explanation
In this benchmark, Lodash's cloneDeep
function and the RFDC custom implementation are used for cloning objects.
Benchmark Results
The latest benchmark results show that:
cloneDeep
provides the slowest execution time (approximately 0.25 seconds per iteration) due to its overhead from being a library function.JSON.parse
method has an intermediate performance, taking around 0.05-0.15 seconds per iteration.Keep in mind that these results might change depending on the input data and specific use cases. It's essential to consider factors like performance, code readability, and maintainability when choosing an approach for your specific needs.