<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 myCopy = null;
myCopy = _.clone(sampleObject);
myCopy = JSON.parse(JSON.stringify(sampleObject));
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 | 1266092.4 Ops/sec |
Json clone | 34585.7 Ops/sec |
RFDC copy | 95280.5 Ops/sec |
Let's break down the benchmark and explain what's being tested.
The benchmark compares the performance of three different methods for cloning an object:
cloneDeep
function from Lodash, a popular JavaScript utility library.JSON.parse(JSON.stringify(sampleObject))
syntax to clone the object.rfdc()
function from the RFDC (Randomized Deep Clone) library.Lodash Clone Deep
JSON Clone
RFDC Copy
In summary, this benchmark compares three different approaches for cloning an object:
The benchmark results show the performance characteristics of each method on a specific test case, allowing developers to make informed decisions about which approach to use in their own applications.