Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer (setAutoFreeze(true)) vs shallow vs ramda lens vs samless immutable111 2
(version: 0)
Comparing performance of:
immer vs shallow copy vs ramda lens vs samless
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/immer/9.0.12/immer.umd.production.min.js" integrity="sha512-3JsFAUa3jXEYKaFyQWG3KFIyx051rsfOMAC4b5+QPjD+CPZ6BC1lhVcAJjyfZBE7YcUXdBEapaGC6hj7wa8+ww==" crossorigin="anonymous" referrerpolicy="no-referrer"></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(true) 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):
**What is being tested?** The provided JSON represents a benchmark test that compares the performance of four different approaches for modifying an immutable data structure: 1. **Immer**: A library for creating and manipulating immutable objects in JavaScript. 2. **Shallow copy**: Creating a new object by copying all properties from an existing object, without modifying the original. 3. **Ramda lens**: A functional programming library that provides a way to access and modify nested data structures using lenses (functions that return a modified version of an input value). 4. **Samless**: Another immutable data structure library for JavaScript. The test creates an initial data structure with 100 items, and then attempts to add a new item to it using each of the four approaches. **Options compared:** * **Immer**: Creates a new, frozen copy of the original data structure using `immer.default` and `immer.setAutoFreeze(true)`. * **Shallow copy**: Creates a new object by copying all properties from the original data structure using the spread operator (`...`). * **Ramda lens**: Uses Ramda's `lensPath` function to access and modify nested data structures. The test uses the `over` function to create a new value for each property that needs to be updated. * **Samless**: Creates a new, immutable data structure by modifying the original using Samless's `setIn` function. **Pros and cons of each approach:** * **Immer**: Pros: + Efficiently creates a new, frozen copy of the original data structure. + Easy to use for simple modifications. Cons: + Requires an extra step to enable freeze mode (`immer.setAutoFreeze(true)`). + Can be slower than other approaches due to its overhead. * **Shallow copy**: Pros: + Quick and easy to implement. + No additional libraries required. Cons: + Creates a new object with all properties, even if only some need to be updated. This can lead to unnecessary memory allocation. + May be slower than other approaches due to the overhead of creating a new object. * **Ramda lens**: Pros: + Provides a powerful and flexible way to access and modify nested data structures. Cons: + Requires knowledge of Ramda's API and syntax. + Can be slower than other approaches due to its overhead. * **Samless**: Pros: + Similar efficiency to Immer for simple modifications. Cons: + Less well-known library compared to Immer or Ramda. **Other considerations:** * Memory allocation: Creating a new, immutable data structure using libraries like Immer or Samless can help prevent memory leaks and performance issues caused by modifying original data structures. * Code readability: Using libraries with clear APIs and documentation (like Ramda lens) can make code more readable and maintainable. However, the added complexity may come at the cost of slower execution times. **Alternatives:** If you're not satisfied with the results or need to compare other approaches, some alternatives include: * **Loose equality checking**: Instead of using Immer or Samless for immutable data structures, consider using loose equality checks (e.g., `const data = {...};`) for small data structures. This can be faster but may lead to memory leaks. * **Merging instead of copying**: For smaller data structures, merging updates into the original data structure instead of creating a new one can be faster and more efficient. * **Higher-order functions**: Using higher-order functions (e.g., `map`, `reduce`) with functional programming libraries like Ramda or Lodash can provide flexible and expressive ways to process data without modifying original structures.
Related benchmarks:
Immer (setAutoFreeze(false)) vs shallow vs ramda lens vs samless immutable111
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
Comments
Confirm delete:
Do you really want to delete benchmark?