<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
var obj = {
"_id": "5a70cbe42440f696758f051f",
"index": 0,
"guid": "020c210f-6943-4e93-9335-822c5f9c72fc",
"isActive": false,
"balance": "$2,608.00",
"picture": "http://placehold.it/32x32",
"age": 21,
"eyeColor": "brown",
"name": "Slater Hodge",
"gender": "male",
"company": "ZORK",
"email": "slaterhodge@zork.com",
"phone": "+1 (905) 499-2902",
"address": "115 Nova Court, Collins, Delaware, 710",
"about": "Ea laborum esse nostrud do excepteur incididunt. Duis dolor fugiat qui sint occaecat occaecat adipisicing amet reprehenderit cupidatat qui do. Exercitation ullamco sint enim pariatur quis duis. Commodo aliqua velit ut laborum velit incididunt veniam est. Culpa officia eu sit duis nostrud deserunt fugiat esse laboris tempor esse consectetur Lorem ipsum.\r\n",
"registered": "2017-09-22T05:08:56 +07:00",
"latitude": 50.226282,
"longitude": -108.795712,
"tags": [
"labore",
"qui",
"tempor",
"officia",
"ad",
"laborum",
"velit"
],
"friends": [
{
"id": 0,
"name": "Carlene Lewis"
},
{
"id": 1,
"name": "Esther Camacho"
},
{
"id": 2,
"name": "Dillon Lancaster"
}
],
"greeting": "Hello, Slater Hodge! You have 2 unread messages.",
"favoriteFruit": "banana"
};
var lodashClone = _.clone(obj);
var nativeClone = {obj};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash clone | |
native clone |
Test name | Executions per second |
---|---|
lodash clone | 102763.1 Ops/sec |
native clone | 2873856.2 Ops/sec |
Let's break down the provided benchmark definition and test cases to understand what is being tested.
Benchmark Definition Overview
The benchmark measures the performance difference between two approaches:
{...obj}
) or Object.assign(), which creates a shallow copy of the original object.clone
function from the Lodash library to create a deep clone of the original object.What is being tested?
The benchmark tests the performance difference between these two approaches:
clone
function to create a deep clone of the original objectOptions Compared
The benchmark compares the following options:
Pros and Cons
Cons:
Library: Lodash
The clone
function from Lodash is used to create a deep clone of the original object. Lodash is a popular JavaScript utility library that provides a range of functions for tasks such as array manipulation, string manipulation, and more.
Special JS Features or Syntax
There are no special JS features or syntax used in this benchmark.
Alternative Approaches
Other approaches to creating clones of objects include:
Object.create()
and then assigning properties from the original object using Object.assign()
.These alternative approaches may have different performance characteristics compared to the native clone and Lodash clone approaches tested in this benchmark.