Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IMMUTABLE COPY
(version: 0)
Comparing performance of:
Immutable vs Lodash vs Manual vs Assign vs so
Created:
3 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://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.min.js'></script> <script> function copyDeep(value) { if (!value) return value; if (value.constructor === Array) { return value.map(copyDeep); } if (value.constructor === Object) { const copy = {}; for (let k in value) { copy[k] = copyDeep(value[k]); } return copy; } return value; } function merge(target, src) { if (!target || !src || target.constructor != Object || src.constructor !== Object) return target; for (var key in src) { if (target[key]) { if (target[key].constructor === Object && src[key].constructor === Object) { target[key] = merge(target[key], src[key]); } else { target[key] = src[key]; } } else { target[key] = src[key]; } } } </script>
Script Preparation code:
var MyObject = { description: 'Creates a deep copy of source, which should be an object or an array.', myNumber: 123456789, myBoolean: true, jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' } }; var myCopy = null;
Tests:
Immutable
myCopy = Immutable.fromJS(MyObject).mergeDeep({ jayson: {parse: 'JSON.parse() method parses a JSON string... 2'}}).toJS()
Lodash
myCopy = _.merge(_.cloneDeep(MyObject), { jayson: {parse: 'JSON.parse() method parses a JSON string... 2'}})
Manual
myCopy = merge(copyDeep(MyObject), { jayson: {parse: 'JSON.parse() method parses a JSON string... 2'}});
Assign
myCopy = Object.assign({}, MyObject, { jayson: {parse: 'JSON.parse() method parses a JSON string... 2'}});
so
myCopy = { ...MyObject, jayson: {parse: 'JSON.parse() method parses a JSON string... 2'}}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Immutable
Lodash
Manual
Assign
so
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 what is being tested in the provided benchmark. **Benchmark Definition** The `benchmark definition` defines how to create a copy of an object `MyObject`. There are four different approaches: 1. **Immutable**: Uses the `Immutable.fromJS()` method to create a deep copy of `MyObject`, and then merges it with another object using `mergeDeep()`. 2. **Lodash**: Uses the `_` library's `cloneDeep()` function to create a deep copy of `MyObject`, and then merges it with another object using `merge()`. 3. **Manual**: Manually creates a deep copy of `MyObject` by recursively iterating over its properties, and then merges it with another object using a custom `copyDeep()` function. 4. **Assign**: Uses the `Object.assign()` method to create a new object that inherits from `MyObject`, and then adds the merged object's properties. **Options compared** Each approach has its pros and cons: * **Immutable**: Pros: Provides a functional programming-style interface, ensures immutability, and is often preferred for data-driven applications. Cons: Can be slower due to the overhead of creating an immutable copy. * **Lodash**: Pros: Provides a robust set of utility functions, including `cloneDeep()` which can handle complex object structures. Cons: Adds additional dependencies, and its API can be overwhelming for some users. * **Manual**: Pros: Can be highly customizable, allows for fine-grained control over the copy process, and often results in faster execution times due to reduced overhead. Cons: Requires manual effort to implement correctly, and can become error-prone if not implemented carefully. * **Assign**: Pros: Simple and intuitive API, widely supported by most JavaScript engines, and efficient for simple object merges. Cons: May not perform well for complex objects or large datasets, as it creates a new object that inherits from the original. **Library usage** The `benchmark` uses two external libraries: * **Immutable.js**: A library for working with immutable data structures in JavaScript. It provides a functional programming-style interface for creating and manipulating immutable data. * **Lodash**: A popular utility library that provides a wide range of functions for working with arrays, objects, and other data structures. **Special JS feature or syntax** There are no special JavaScript features or syntaxes being used in this benchmark. The focus is on comparing the performance of different object copy and merge approaches. **Other alternatives** If you wanted to compare these approaches without using Immutable.js or Lodash, you could implement your own custom `copyDeep()` function for the Manual approach, or use a different library like `JSON.parse()` and `JSON.stringify()` for merging objects (although this would likely result in slower performance due to the overhead of parsing and stringifying JSON).
Related benchmarks:
Lodash cloneDeep vs JSON Clone
Lodash cloneDeep vs clone
Lodash cloneDeep vs JSON Clone with Array
Lodash cloneDeep vs JSON Clone with String
Lodash cloneDeep VS mutate
Comments
Confirm delete:
Do you really want to delete benchmark?