<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js'></script>
var MyObject = {"author":{},"attrs":{"author_name":null},"event_question_id":4175637,"event_id":423443,"event_section_id":489248,"text":"TESTUJEMEMEEEEEEEEEEE","is_public":false,"is_answered":false,"is_highlighted":false,"is_anonymous":true,"is_bookmarked":false,"score":0,"score_positive":0,"score_negative":0,"date_published":null,"date_highlighted":null,"path":"/questions","date_created":"2018-04-12T14:29:51.000Z","date_updated":"2018-04-12T14:29:51.000Z","date_deleted":null,"media":[]};
var myCopy = null;
myCopy = _.cloneDeep(MyObject);
myCopy = JSON.parse(JSON.stringify(MyObject));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
Json clone |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 138530.4 Ops/sec |
Json clone | 116684.1 Ops/sec |
Let's break down the provided benchmark definition and test cases to understand what is being tested.
Benchmark Definition
The benchmark consists of two individual test cases:
_.cloneDeep
from Lodash to create a deep copy of an object (MyObject
). Lodash is a popular JavaScript library that provides a collection of useful functions for functional programming.MyObject
using JSON.parse(JSON.stringify(MyObject))
. This approach relies on the JSON.stringify method to serialize the object and then parsing it back into an object.What is tested
In both test cases, we're testing how quickly each approach can create a new copy of the original MyObject
without modifying the original data. The benchmark measures the number of executions per second (ExecutionsPerSecond) for each browser and device platform.
Options compared
The two options being compared are:
_.cloneDeep
function from Lodash to create a deep copy of the object.JSON.parse(JSON.stringify(MyObject))
to create a deep clone of the object.Pros and Cons
Here's a brief summary of each approach:
Library used
In both test cases, Lodash is used as a library to provide the _.cloneDeep
function. Lodash's cloneDeep
function is designed to create deep copies of objects, which can be useful in various scenarios, such as object merging or data transformation.
Special JS feature or syntax
There are no special JavaScript features or syntax mentioned in this benchmark. Both test cases use standard JavaScript syntax and libraries (Lodash).
Other alternatives
If you're looking for alternative approaches to create a deep clone of an object without using Lodash, you could consider:
Object.assign()
to merge a copy of the original object with itself, effectively creating a new copy.Keep in mind that these alternatives might not be as efficient or reliable as Lodash's cloneDeep
function, especially for complex data structures.
In summary, this benchmark compares two approaches to create a deep clone of an object: using Lodash's _.cloneDeep
function versus JSON.parse(JSON.stringify(MyObject))
. The choice between these options depends on the specific requirements and constraints of your project.