<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
const base = {
firstName: 'John',
lastName: 'Doe',
size: 170,
birthDate: new Date(1985, 0,15),
married: true,
address: {
street: '2 av Victor Hugo',
country: 'FR',
},
childrens: [
{
name: 'Paul',
age: 8,
},
],
functions: [
function () { return 'Base function' },
],
};
const copy = JSON.parse(JSON.stringify(base));
function clone(aObject) {
// Prevent undefined objects
// if (!aObject) return aObject;
let bObject = Array.isArray(aObject) ? [] : {};
let value;
for (const key in aObject) {
// Prevent self-references to parent object
// if (Object.is(aObject[key], aObject)) continue;
value = aObject[key];
bObject[key] = (typeof value === "object") ? clone(value) : value;
}
return bObject;
};
// base object
const base = {
firstName: 'John',
lastName: 'Doe',
size: 170,
birthDate: new Date(1985, 0,15),
married: true,
address: {
street: '2 av Victor Hugo',
country: 'FR',
},
childrens: [
{
name: 'Paul',
age: 8,
},
],
functions: [
function () { return 'Base function' },
],
};
const copy = clone(base);
const base = {
firstName: 'John',
lastName: 'Doe',
size: 170,
birthDate: new Date(1985, 0,15),
married: true,
address: {
street: '2 av Victor Hugo',
country: 'FR',
},
childrens: [
{
name: 'Paul',
age: 8,
},
],
functions: [
function () { return 'Base function' },
],
};
const copy = _.cloneDeep(base);
const base = {
firstName: 'John',
lastName: 'Doe',
size: 170,
birthDate: new Date(1985, 0,15),
married: true,
address: {
street: '2 av Victor Hugo',
country: 'FR',
},
childrens: [
{
name: 'Paul',
age: 8,
},
],
functions: [
function () { return 'Base function' },
],
};
const copy = jQuery.extend(true, {}, base);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
JSON.parse(JSON.stringify()) | |
Custom function | |
Lodash.cloneDeep() | |
jQuery.extend(true) |
Test name | Executions per second |
---|---|
JSON.parse(JSON.stringify()) | 244468.1 Ops/sec |
Custom function | 509495.2 Ops/sec |
Lodash.cloneDeep() | 371343.7 Ops/sec |
jQuery.extend(true) | 410452.3 Ops/sec |
Let's dive into the world of benchmarking and explore what's being tested on MeasureThat.net.
Benchmark Definition
The benchmark is designed to compare the performance of four different methods for cloning an object:
...
): This method uses the spread operator to create a new copy of the original object.JSON.parse(JSON.stringify())
: This method uses JSON serialization and deserialization to clone the object.cloneDeep()
: This method is a part of the popular Lodash library, which provides a function for deeply cloning objects.Options Compared
The benchmark compares the performance of these four methods, including:
Pros and Cons of Each Method
Here's a brief overview of each method's pros and cons:
...
):JSON.parse(JSON.stringify())
:cloneDeep()
:Benchmark Results
The latest benchmark results show the performance differences between these methods:
cloneDeep()
: 371343.65625 executions per secondJSON.parse(JSON.stringify())
: 244468.109375 executions per secondAs expected, the custom function and jQuery.extend(true) perform relatively well, while Lodash's cloneDeep()
and JSON.parse(JSON.stringify())
are slower due to their additional overhead.
In conclusion, this benchmark provides valuable insights into the performance characteristics of different object cloning methods. While the spread operator is simple and efficient for small objects, it may not be suitable for complex cases. The custom function and jQuery.extend(true) provide a good balance between control and performance, while Lodash's cloneDeep()
is a reliable but slower option due to its added dependency.