Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer (setAutoFreeze(false)) vs shallow vs ramda lens vs samless immutable
(version: 0)
Comparing performance of:
immer vs shallow copy vs ramda lens
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://unpkg.com/immer/dist/immer.umd.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
Script Preparation code:
var { compose, over, set, lensPath, append } = R var INITIAL_DATA = { items: {}, count: 0, keys: [] } for (var index = 0; index < 100; index++) { INITIAL_DATA[index] = { id: index, name: `ITEM-${index}`, value: Math.random() } INITIAL_DATA.count++ INITIAL_DATA.keys.push(index) } var NEW_ITEM_ID = INITIAL_DATA.count +1 var produce = immer.default immer.setAutoFreeze(false)
Tests:
immer
data = produce(INITIAL_DATA, draft => { draft.items[NEW_ITEM_ID] = { id: NEW_ITEM_ID, name: 'ITEM-NEW', value: 0 } draft.counter++ draft.keys.push(NEW_ITEM_ID) })
shallow copy
data = { ...INITIAL_DATA, items: { ...INITIAL_DATA.items, [NEW_ITEM_ID]: { id: NEW_ITEM_ID, name: 'ITEM-NEW', value: 0 } }, count: INITIAL_DATA.count +1, keys: [ ...INITIAL_DATA.keys, NEW_ITEM_ID] }
ramda lens
data = compose( over(lensPath(["keys"]), append(NEW_ITEM_ID)), over(lensPath(["count"]), x => x + 1), set(lensPath(["items", NEW_ITEM_ID]), { id: NEW_ITEM_ID, name: "ITEM-NEW", value: 0 }) )(INITIAL_DATA);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
immer
shallow copy
ramda lens
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):
I'll break down the benchmark and explain what's being tested, compared options, pros and cons, and other considerations. **Benchmark Overview** The test compares four ways to update an object in JavaScript: 1. **Immer**: A library that provides a simple and efficient way to work with immutable objects. 2. **Shallow copy**: Creating a new object by copying the properties of the original object. 3. **Ramda Lens**: A functional programming library that provides a way to compose updates to an object using a path-based syntax. 4. **Samless Immutable**: Not mentioned in the provided data, but assumed to be another approach ( possibly a custom implementation). **Benchmark Setup** The benchmark uses a predefined dataset (`INITIAL_DATA`) and a script preparation code that initializes some variables and creates a new item with `NEW_ITEM_ID`. The HTML preparation code includes links to the necessary libraries: `immer.umd.js` and `ramda.min.js`. **Test Cases** There are three test cases: 1. **Immer**: Creates an immutable object using `immer.default` and updates it by creating a new version of the object (`produce`) with a function that modifies the original data. 2. **Shallow copy**: Creates a shallow copy of the initial data and updates it by spreading the existing properties and adding a new property for the new item. 3. **Ramda Lens**: Uses Ramda's `lensPath` and `over` functions to compose an update function that modifies the object. **Comparison Options** The benchmark compares the performance of each approach: * **Immer**: Updates the original object by creating a new version with the modifications applied using `immer.default`. * **Shallow copy**: Creates a new object by copying the properties of the original object and then updates it. * **Ramda Lens**: Composes an update function using Ramda's `lensPath` and `over` functions to access and modify specific parts of the object. **Pros and Cons** Here are some pros and cons of each approach: * **Immer**: + Pros: Efficient, easy to use, and provides a simple way to work with immutable objects. + Cons: May incur a performance overhead due to the creation of multiple versions of the object. * **Shallow copy**: + Pros: Easy to implement, can be efficient for small objects. + Cons: Can lead to unexpected behavior if not handled carefully (e.g., modifying properties on the original object). * **Ramda Lens**: + Pros: Provides a functional programming approach that can be composable and reusable. + Cons: May have a steeper learning curve, and performance can be affected by the complexity of the composed functions. **Other Considerations** * **Immutable objects**: Using immutable objects can help ensure thread safety and reduce side effects in concurrent environments. * **Performance overhead**: Creating multiple versions of an object (e.g., with Immer) can incur a performance overhead, especially for large datasets. * **Memory usage**: Shallow copying an object can lead to increased memory usage if the new object is not properly garbage collected. **Alternatives** Some alternative approaches to updating objects in JavaScript include: * **ES6 spread operator**: Using the spread operator (`{...obj}`) to create a shallow copy of an object. * **`Object.assign()`**: Using `Object.assign()` to update properties on an existing object. * **Custom implementation**: Creating a custom approach that suits specific requirements, such as using a library like Lodash or Mujs.
Related benchmarks:
Immer (setAutoFreeze(false)) vs shallow vs ramda lens vs samless immutable111
Immer (setAutoFreeze(false)) vs shallow vs ramda lens vs immutabe js vs immutable js (toJS) vs mutating 2.0
Immer vs shallow vs ramda lens (2)
Immer (setAutoFreeze(false)) vs shallow vs ramda lens vs samless immutable111 2
Immer (setAutoFreeze(true)) vs shallow vs ramda lens vs samless immutable111 2
Comments
Confirm delete:
Do you really want to delete benchmark?