Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer vs shallow vs ramda lens (2)
(version: 0)
Comparing performance of:
immer vs shallow copy vs ramda lens
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://unpkg.com/immer@9.0.7/dist/immer.umd.production.min.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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 135 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
immer
10198.7 Ops/sec
shallow copy
971910.8 Ops/sec
ramda lens
50109.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark definition represents three different approaches to create a copy of an object: `immer`, `shallow copy`, and `ramda lens`. 1. **Immer**: Immer is a library that provides a simple way to create immutable objects in JavaScript. The benchmark uses the `produce` function from Immer to create a new, deep copy of the initial data. 2. **Shallow Copy**: A shallow copy creates a new object with references to the same values as the original object. In this case, it's used to create a new object with a single property (`items`) that contains all the properties of the original `items` object, and then adds a new item to it. 3. **Ramda Lens**: Ramda is a functional programming library that provides several useful functions for manipulating data. The benchmark uses the `lensPath` function to create a lens (a way to focus on specific parts of an object) and then applies a series of operations using the `over` and `set` functions to update the original data. **Comparison** The comparison is between these three approaches: * **Immer**: Creates a deep copy of the entire object, which can be slower for large objects due to the overhead of creating a new object. * **Shallow Copy**: Creates a shallow copy by referencing the same values as the original object. This approach is faster but can lead to unexpected behavior if one of the referenced values changes. * **Ramda Lens**: Uses a combination of lenses and functions to update specific parts of the object without creating a new copy. This approach requires more code and may be slower than Immer or shallow copy. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **Immer**: * Pros: Fast, creates a deep copy, easy to use. * Cons: May consume more memory, can be slow for large objects. 2. **Shallow Copy**: * Pros: Fast, lightweight, simple. * Cons: Can lead to unexpected behavior if referenced values change. 3. **Ramda Lens**: * Pros: Flexible, allows for fine-grained control over updates. * Cons: Requires more code, may be slower. **Library and Purpose** * **Immer**: A library that provides a simple way to create immutable objects in JavaScript. * **Ramda**: A functional programming library that provides several useful functions for manipulating data. **Special JS Feature or Syntax** None of the above benchmarks use special JavaScript features or syntax.
Related benchmarks:
Immer vs shallow vs ramda lens
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
ramda vs immer with fixed setup
Comments
Confirm delete:
Do you really want to delete benchmark?