Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer (setAutoFreeze(false)) 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(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 dive into the world of JavaScript microbenchmarks. The provided benchmark, MeasureThat.net, tests four different approaches to update and modify an object in JavaScript: 1. **Immer (setAutoFreeze(false))**: This approach uses the Immer library to create a new object copy when updating the original object. The `setAutoFreeze` function is set to `false`, which means that Immer will not automatically freeze the object's prototype chain. 2. **Shallow Copy**: This approach creates a shallow copy of the original object using the spread operator (`{...INITIAL_DATA, ...}`). 3. **Ramda Lens**: This approach uses the Ramda library to update specific parts of the object using lenses (a concept from functional programming). The `over` function is used to update the values of specific properties. 4. **Samless**: This approach uses the Samless immutable data structure to create a new, updated version of the original object. **Pros and Cons:** * **Immer (setAutoFreeze(false))**: + Pros: efficient updates, easy to use, supports deep copies + Cons: may not be suitable for very large objects, can lead to memory leaks if not used carefully * **Shallow Copy**: + Pros: simple and lightweight, easy to implement + Cons: creates a new object reference, which can lead to unexpected behavior in some cases * **Ramda Lens**: + Pros: powerful and flexible, supports complex updates + Cons: steeper learning curve, may be slower than Immer for small updates * **Samless**: + Pros: provides strong guarantees about immutability, easy to use + Cons: can be slow and memory-intensive, requires more overhead **Library Descriptions:** * **Immer**: A library that makes it easy to work with immutable objects in JavaScript. It creates a new object copy when updating the original object. * **Ramda**: A functional programming library for JavaScript that provides a wide range of utilities and functions for working with data. * **Samless**: An immutable data structure library for JavaScript that provides a simple and efficient way to create and manipulate immutable objects. **Special JS Features/Syntax:** This benchmark does not use any special JavaScript features or syntax, such as async/await, arrow functions, or class declarations. However, it does rely on modern JavaScript features like template literals and destructuring. **Other Alternatives:** * **Lodash**: A utility library that provides a wide range of functions for working with data in JavaScript. * **Mochas**: A benchmarking library for JavaScript that allows you to write custom benchmarks using a simple syntax. * **Benchmark.js**: A fast, accurate, and easy-to-use benchmarking library for JavaScript. In summary, the Immer approach is likely to be the fastest and most efficient way to update an object in this benchmark, followed closely by Samless. The Shallow Copy approach is simpler but may not provide the same level of performance or immutability as the other approaches. Ramda Lens provides a powerful and flexible way to update objects, but may require more overhead and learning time.
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(true)) vs shallow vs ramda lens vs samless immutable111 2
Comments
Confirm delete:
Do you really want to delete benchmark?