Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs JSON Clone 6
(version: 3)
Comparing performance of:
Deep vs JSON
Created:
6 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var MyObject = { categoryName: '', mx_internal_uid: 1, isCategory: false, id: '', sourceId: '', sourceType: '', name: '', phone1: '', phoneOneCarrier: '', phone2: '', phoneTwoCarrier: '', email1: '', email2: '', emailNotification: null, smsNotification: null, contactGroupList: [], terminalList: [] }; var myCopy = null;
Tests:
Deep
myCopy = _.cloneDeep(MyObject);
JSON
myCopy = JSON.parse(JSON.stringify(MyObject));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Deep
JSON
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark is comparing two approaches to clone (copy) an object: `lodash.cloneDeep` and `JSON.parse(JSON.stringify(obj))`. The benchmark aims to measure which approach is faster for deep object cloning. **Options compared** There are two options being compared: 1. **Lodash `cloneDeep`**: A function from the Lodash library that creates a shallow copy of an object with the result of recursively calling `cloneDeep` on each detected cyclic reference. 2. **JSON.parse(JSON.stringify(obj))**: A built-in JavaScript method that clones an object by serializing it to JSON and then parsing the resulting string back into a new object. **Pros and Cons** 1. **Lodash `cloneDeep`**: * Pros: + Can handle cyclic references, which can occur when objects reference each other. + Provides more control over the cloning process through its options. * Cons: + Requires the Lodash library to be included in the test environment. 2. **JSON.parse(JSON.stringify(obj))**: * Pros: + Is a built-in JavaScript method, so no additional libraries are required. + Can handle cyclic references, but may not be as efficient as `cloneDeep`. * Cons: + Does not provide control over the cloning process. **Library and its purpose** The Lodash library is a popular utility library for JavaScript that provides a wide range of functions for tasks like array manipulation, object manipulation, and more. In this benchmark, `lodash.cloneDeep` is used to create a deep copy of an object. **Other considerations** * The benchmark uses Chrome 80 as the test browser, which may affect the results due to differences in JavaScript engine or browser-specific optimizations. * The benchmark does not account for potential differences in performance between shallow and deep cloning. If the cloned objects are only used with shallow properties, `JSON.parse(JSON.stringify(obj))` might be sufficient and faster. **Alternatives** If you need to clone an object, but do not want to use a library like Lodash or rely on built-in methods, you can consider using: * The `Object.assign()` method with a deep option (`Object.assign(target, source, options)`), which creates a new object by copying the specified properties from one or more source objects. * The `Array.prototype.slice()` and `Array.prototype.concat()` methods to clone arrays. However, keep in mind that these alternatives may not provide the same level of control as `cloneDeep` or handle cyclic references as efficiently.
Related benchmarks:
Lodash cloneDeep vs clone vs spread
Lodash cloneDeep vs JSON Clone with Array
Lodash cloneDeep vs JSON Clone with String
lodash cloneDeep vs json.stringify
Lodash cloneDeep vs JSON Clone vs freeze and get - access a value
Comments
Confirm delete:
Do you really want to delete benchmark?