Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer (setAutoFreeze(false)) 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 immer.setAutoFreeze(false)
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 benchmark and explain what's being tested. **Benchmark Overview** The benchmark is comparing three approaches to update an object in JavaScript: 1. Immer (setAutoFreeze(false)) 2. Shallow copy 3. Ramda lens **Immer (setAutoFreeze(false))** Immer is a library that provides a safe and efficient way to work with immutable data structures. The `setAutoFreeze(false)` option tells Immer not to automatically freeze the object when it's first created, which can lead to performance issues. In this benchmark, Immer is used to update an object by creating a new draft and mutating it in place. This approach requires Immer to perform additional checks and balances, which may impact performance. **Shallow Copy** The shallow copy approach involves creating a new object that contains a reference to the original data structure. When updating the copied object, changes are made directly to the original data structure. This approach can be faster than Immer because it avoids the overhead of creating a new draft and performing mutations in place. However, the shallow copy approach can lead to unexpected behavior if the original data structure is modified elsewhere in the code. **Ramda Lens** Ramda lens is a library that provides a functional programming style for working with objects. The `lensPath` function allows you to update specific properties of an object without affecting other parts of the data structure. In this benchmark, Ramda lens is used to update an object by composing multiple lens functions together. This approach can be more efficient than Immer because it avoids creating a new draft and performing mutations in place. However, the Ramda lens approach requires more boilerplate code to set up and use, which may impact performance. **Pros and Cons** Here's a summary of the pros and cons of each approach: | Approach | Pros | Cons | | --- | --- | --- | | Immer (setAutoFreeze(false)) | Safe and efficient | May require additional checks and balances, leading to slower performance | | Shallow Copy | Faster than Immer | May lead to unexpected behavior if original data structure is modified elsewhere in the code | | Ramda Lens | Efficient and functional programming style | Requires more boilerplate code and may be less familiar to developers | **Library Usage** The benchmark uses three libraries: * Immer: a library for working with immutable data structures * Ramda: a functional programming library that provides the `lensPath` function Both libraries are widely used in the JavaScript community, but may have different performance characteristics depending on the specific use case. **Special JS Feature or Syntax** The benchmark uses the `const` keyword to declare variables and functions, which is a modern feature introduced in ECMAScript 2015. It also uses arrow functions (`=>`) and template literals (`\r\n`) for simplicity and readability. Overall, this benchmark provides a useful comparison of three approaches to updating objects in JavaScript, highlighting their strengths and weaknesses.
Related benchmarks:
Immer vs shallow vs ramda lens
Immer (setAutoFreeze(false)) vs shallow vs ramda lens vs samless immutable111
Immer vs shallow vs ramda lens (2)
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?