Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
When use Immutable
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Immutable toJS
Created:
7 years ago
by:
Guest
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 src="https://cdn.jsdelivr.net/immutable.js/4.0.0-rc.2/immutable.min.js"></script>
Script Preparation code:
var MyObject = { description: 'a', myNumber: 123456789, myBoolean: true, inner: { description: 'a', myNumber: 123456789, myBoolean: true, inner: { description: 'a', myNumber: 123456789, myBoolean: true, inner: { description: 'a', myNumber: 123456789, myBoolean: true, inner: { description: 'a', myNumber: 123456789, myBoolean: true, inner: { description: 'a', myNumber: 123456789, myBoolean: true, inner: { description: 'a', myNumber: 123456789, myBoolean: true, } } } } } } }; var MyMap = Immutable.fromJS(MyObject); var myCopy = null;
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
Immutable toJS
//myCopy = {...MyObject}; myCopy = MyMap.toJS();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Immutable toJS
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 benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark measures the performance difference between two approaches: using Lodash's `cloneDeep` function to create a deep copy of an object, and using Immutable.js's `toJS()` method to convert an Immutable object into a plain JavaScript object. **Script Preparation Code** The preparation code creates an immutable object `MyObject` with nested properties. The script also initializes two variables: `myCopy` (initialized as null), which will be used for the first test, and `MyMap`, which is created by converting `MyObject` to an Immutable object using `Immutable.fromJS()`. This setup allows us to compare the performance of creating a deep copy with Lodash versus converting an Immutable object to a plain JavaScript object. **Html Preparation Code** The HTML code includes scripts for Lodash (version 4.17.5) and Immutable.js (version 4.0.0-rc.2). These libraries are used by the benchmark tests. **Individual Test Cases** There are two test cases: 1. **Lodash cloneDeep** * The `Benchmark Definition` script uses `_.cloneDeep(MyObject)` to create a deep copy of `MyObject`. * This function creates a new object with all nested properties recursively cloned. 2. **Immutable toJS** * The `Benchmark Definition` script uses `myCopy = MyMap.toJS()` to convert the Immutable object `MyMap` into a plain JavaScript object. * This method converts the Immutable object's data structure into a plain JavaScript object, effectively creating a shallow copy of its nested properties. **Pros and Cons** 1. **Lodash cloneDeep** * Pros: + Deep copies are more accurate (i.e., they preserve all nested properties). + Can be used for complex data structures. * Cons: + Slower performance due to the recursive cloning process. 2. **Immutable toJS** * Pros: + Faster execution time since it only converts the top-level data structure, not recursively cloning all nested properties. + Uses Immutable.js's optimized data structure algorithms. * Cons: + May lose some precision in deeply nested objects (i.e., may discard some of the innermost values). + Not suitable for complex data structures. **Device and Browser Variations** The benchmark uses a specific Chrome 71 browser running on a Macintosh with Intel hardware, which might not be representative of all possible devices and browsers. The results should be taken into account when comparing performance across different environments. **Alternatives** Other approaches could be used to create copies or conversions: 1. **Plain JavaScript Object Creation** * `const myCopy = { ...MyObject };` (shallow copy) * `const myCopy = JSON.parse(JSON.stringify(MyObject));` (deep copy) 2. **Other Immutable.js Methods** * `myCopy = MyMap.toList()` or `myCopy = MyMap.toArray()` for shallow conversions 3. **Other Deep Copy Libraries** * e.g., `lodash-es` with its `cloneDeepES6` function In conclusion, the benchmark measures the trade-off between deep copying and converting Immutable objects to plain JavaScript objects. Lodash's `cloneDeep` provides more accurate results but is slower, while Immutable.js's `toJS()` method offers faster execution times at the cost of some precision in deeply nested objects.
Related benchmarks:
Test immutables data for map operation with Object.values()
immutable vs lodash vs ... 4
immutable vs lodash vs ... 5
POJOs vs Immutable Maps
immutable vs Native Javascript Map With Read
Comments
Confirm delete:
Do you really want to delete benchmark?