Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer vs shallow vs ramda lens
(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
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 definition and test cases. **Overview** The benchmark measures the performance of three different approaches to update an object: 1. **Immer**: uses the Immer library to create a draft copy of the initial data, allowing for safe and efficient updates. 2. **Shallow Copy**: creates a shallow copy of the initial data using the spread operator (`{...INITIAL_DATA, ...}`). 3. **Ramda Lens**: uses Ramda's lens library to update specific parts of the object. **Immer** * Immer is a library that provides an efficient and safe way to create immutable copies of objects. * The benchmark definition uses `immer.default` to create a draft copy of the initial data, which allows for updates without modifying the original object. * The `produce` function creates a new, updated version of the initial data, while leaving the original unchanged. Pros: * Immer provides a safe and efficient way to update objects, making it ideal for applications that require predictable behavior. * The use of a draft copy minimizes the risk of side effects or unexpected changes to the original data. Cons: * Immer can introduce additional overhead due to the creation of a new object and the management of the draft copy. * The benchmark results show that Immer is slightly slower than Shallow Copy, but still outperforms Ramda Lens. **Shallow Copy** * This approach creates a shallow copy of the initial data using the spread operator (`{...INITIAL_DATA, ...}`). * The updated version of the data is created by modifying the copied object directly. * Since this is a shallow copy, any nested objects are shared between the original and updated data. Pros: * Shallow Copy is generally faster than Immer since it doesn't require creating a new object or managing a draft copy. * This approach can be beneficial when working with large datasets or complex objects where sharing nested objects makes sense. Cons: * Shallow Copy does not provide the same level of safety and predictability as Immer, as updates can have unintended side effects on the original data. * The benchmark results show that Shallow Copy is slower than Immer but faster than Ramda Lens. **Ramda Lens** * This approach uses Ramda's lens library to update specific parts of the object. * The `compose` and `over` functions are used to create a chain of updates that transform the initial data. * Each update applies a transformation to the current value, resulting in a new, updated version of the data. Pros: * Ramda Lens provides a flexible and powerful way to update objects by allowing for precise control over which parts of the object are modified. * This approach can be beneficial when working with complex data structures or when specific updates require careful consideration. Cons: * Ramda Lens introduces additional complexity due to the use of lens functions, which can lead to slower performance compared to Immer and Shallow Copy. * The benchmark results show that Ramda Lens is the slowest approach among the three, likely due to the overhead introduced by the lens library. **Other Considerations** When choosing between these approaches, consider the specific requirements of your application: * If predictability and safety are crucial, use Immer. * If performance is a top priority and sharing nested objects makes sense, use Shallow Copy. * If precise control over updates is required or when working with complex data structures, use Ramda Lens. Keep in mind that these approaches have different trade-offs, and the best choice ultimately depends on your specific use case.
Related benchmarks:
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?