Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer vs shallow vs ramda lens by d
(version: 0)
Comparing performance of:
immer vs shallow copy vs ramda lens
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://unpkg.com/immer"></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
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):
Let's break down the provided benchmark and explain what is being tested, the different approaches compared, their pros and cons, and other considerations. **What is being tested?** The benchmark tests three different approaches to create a new item in an object: 1. **Immer**: A library that provides a simple and efficient way to work with immutable data structures. 2. **Shallow copy**: A manual approach to create a new object by copying existing properties using the spread operator (`{ ...INITIAL_DATA, ... }`). 3. **Ramda lens**: A functional programming library that provides a powerful way to update nested objects using lenses (functions that focus on specific parts of an object). **Approaches compared** The three approaches are compared in terms of their performance, which is measured by the number of executions per second. * **Immer**: Uses the `immer.default` function to create a new draft of the data structure. * **Shallow copy**: Manually creates a new object using the spread operator (`{ ...INITIAL_DATA, ... }`) and then updates it with new properties. * **Ramda lens**: Uses Ramda's `compose`, `over`, `lensPath`, and `set` functions to update specific parts of the data structure. **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 data structures. + Cons: May not be suitable for large or complex data structures due to its simplicity. * **Shallow copy**: + Pros: Can be used with any JavaScript object, no dependencies required. + Cons: May lead to unnecessary object creations and copies, especially for large datasets. * **Ramda lens**: + Pros: Provides a powerful way to update nested objects using lenses, flexible, and efficient. + Cons: Requires knowledge of functional programming concepts and Ramda's API. **Other considerations** Other factors that might affect performance or usage include: * **Object size**: Larger objects may benefit from more efficient approaches like Immer or Ramda lens. * **Data structure complexity**: More complex data structures (e.g., trees, graphs) might require specialized libraries or approaches. * **JavaScript engine optimizations**: Some JavaScript engines may optimize certain approaches or libraries over others. **Library descriptions** The benchmark uses the following libraries: * **Immer**: A library for working with immutable data structures. Provides a simple and efficient way to create new draft objects. * **Ramda**: A functional programming library that provides a powerful way to update nested objects using lenses (functions that focus on specific parts of an object). **Special JS features or syntax** The benchmark uses the following special JavaScript features or syntax: * **Arrow functions**: Used in the `immer.default` function and Ramda's `compose`, `over`, `lensPath`, and `set` functions. * **Spread operator**: Used to create shallow copies of objects. I hope this explanation helps software engineers understand what is being tested in the benchmark, the different approaches compared, their pros and cons, and other considerations.
Related benchmarks:
Immer vs shallow vs ramda lens
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
ramda vs immer with fixed setup
Comments
Confirm delete:
Do you really want to delete benchmark?