Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
State update
(version: 0)
Comparing performance of:
Immer vs Spread Operator vs Ramda Lens
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/ramda@0.26.1/dist/ramda.min.js" integrity="sha256-43x9r7YRdZpZqTjDT5E0Vfrxn1ajIZLyYWtfAXsargA=" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/immer@4.0.1/dist/immer.umd.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) })
Spread Operator
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
Spread Operator
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):
**Overview of the Benchmark** The provided benchmark measures the performance of different approaches for updating data in an object. The test case creates an initial data structure with 100 items and then updates it by adding a new item. **Test Cases** There are three individual test cases: 1. **Immer**: This approach uses the Immer library to create a draft version of the data object and then updates it. 2. **Spread Operator**: This approach uses the spread operator (`...`) to create a new object with the updated values. 3. **Ramda Lens**: This approach uses the Ramda library's lens function to update specific properties of the data object. **Library Used** * Immer: A library for functional updates of objects, providing a safe and predictable way to mutate objects without affecting their original state. * Ramda: A functional programming library that provides a set of higher-order functions for manipulating data. **Special JS Features or Syntax** None mentioned in the benchmark definition. However, it's worth noting that Immer uses a special syntax for creating draft versions of objects, which is not part of standard JavaScript. **Approaches Compared** The three approaches compared are: * **Immer**: Uses a functional update approach to update data, providing a safe and predictable way to mutate objects. + Pros: Provides a safe and predictable way to update data, can be used for both simple and complex updates. + Cons: Can be slower than other approaches due to the overhead of creating draft versions. * **Spread Operator**: Uses the spread operator (`...`) to create a new object with updated values. + Pros: Fast and efficient, easy to use. + Cons: Does not provide a safe way to update data, can lead to unintended side effects if not used carefully. * **Ramda Lens**: Uses Ramda's lens function to update specific properties of the data object. + Pros: Provides a fine-grained control over updates, can be used for complex updates. + Cons: Can be slower than other approaches due to the overhead of creating and applying lens functions. **Other Alternatives** There are other approaches that could be used to update data in an object, such as: * **Object.assign()**: A built-in JavaScript method for updating objects. * **Prototype chaining**: A way of using the prototype chain to access and modify properties of objects. * **Mutating arrays**: Using array methods like `push()` or `splice()` to update arrays. However, these approaches may not provide the same level of safety and predictability as Immer or Ramda Lens, especially for complex updates.
Related benchmarks:
Immer vs shallow vs ramda lens
Immer (setAutoFreeze(false)) vs Lodash cloneDeep 3
Immer (setAutoFreeze(false)) vs Lodash cloneDeep 22
ramda vs immer with fixed setup
Comments
Confirm delete:
Do you really want to delete benchmark?