Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer (setAutoFreeze(false)) vs shallow vs ramda lens vs samless immutable111
(version: 2)
Comparing performance of:
immer vs shallow copy vs ramda lens vs samless
Created:
7 years ago
by:
Registered User
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 src="https://unpkg.com/seamless-immutable@7.1.4/seamless-immutable.production.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.items[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) var INITIAL_DATA_I = Immutable({ items: INITIAL_DATA.items, count: INITIAL_DATA.count, keys: Immutable(INITIAL_DATA.keys)})
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);
samless
data = INITIAL_DATA_I .setIn(['keys'], NEW_ITEM_ID) .setIn(['count'],INITIAL_DATA_I.count +1) .setIn(['items', NEW_ITEM_ID], { id: NEW_ITEM_ID, name: 'ITEM-NEW', value: 0 })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
immer
shallow copy
ramda lens
samless
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 benchmark compares four approaches to create a copy of an object: 1. **Shallow Copy**: A simple assignment of an object, without any modifications to the original data. 2. **Immer (setAutoFreeze(false))**: Immer is a library that provides a functional way to work with immutable data structures. By setting `setAutoFreeze(false)`, we create a draft version of the data that can be modified. 3. **Ramda Lens**: Ramda is a functional programming library, and Lens is a specific function that creates a new value by applying transformations to an existing one. In this case, it's used to create a new object with the updated values. 4. **Samless**: Samless is another immutable data structure library, similar to Immer. Now, let's look at each test case in more detail: * **Immer**: The benchmark creates a draft version of the initial data using `immer.default`, and then modifies it by adding a new item. The result is compared with the original data. * **Shallow Copy**: The benchmark simply assigns the initial data to a new variable, without making any modifications. * **Ramda Lens**: The benchmark uses Ramda's Lens function to create a new object with the updated values. It starts with the original data and applies transformations using `over` and `set`. * **Samless**: The benchmark creates an immutable copy of the initial data using Samless, and then modifies it by adding a new item. Let's discuss the pros and cons of each approach: **Shallow Copy** Pros: Simple and fast, no additional libraries required. Cons: Modifies the original data, which might not be desirable in some cases. **Immer (setAutoFreeze(false))** Pros: Provides an immutable copy of the data, allows for efficient modification without creating a new object. Cons: Requires setting up the Immer library and defining the draft version. **Ramda Lens** Pros: Flexible and powerful way to create new values from existing ones, no additional libraries required for basic operations. Cons: Can be slower due to the overhead of Ramda's functions, requires knowledge of Ramda's API. **Samless** Pros: Similar to Immer, provides an immutable copy of the data and allows for efficient modification without creating a new object. Cons: Might require more setup than Immer, less popular library compared to Ramda. Other considerations: * **Modifying original data**: Some approaches (Shallow Copy) modify the original data, while others (Immer, Samless) create immutable copies. This might be important depending on the use case. * **Performance**: The benchmark results show that Immer and Samless are generally faster than Ramda Lens and Shallow Copy. However, this might depend on the specific use case and implementation details. Alternatives to these libraries and approaches: * For creating shallow copies, you can simply use the spread operator (`{...initialData}`) or `Object.assign()`. * For immutable data structures, consider using libraries like Immutable.js, which is similar to Immer and Samless. * For functional programming and transformation functions, Ramda's API might be more familiar and convenient.
Related benchmarks:
Immer (setAutoFreeze(false)) vs shallow vs ramda lens vs immutable js
Immer (setAutoFreeze(false)) vs shallow vs ramda lens vs immutabe js vs immutable js (toJS) vs mutating 2.0
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?