Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer vs shallow vs ramda lens (nested structure)
(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 { set, lensPath } = R var object = { one: 1, two: 2, three: 3, four: 4, five: 5, array: Array.from({ length: 10000 }).map(() => Math.random()) } var INITIAL_DATA = { one: { two: { three: { four: { five: 5, object }, object }, object }, object }, two: { three: { four: { five: 5, object }, object }, object }, three: { four: { five: 5, object }, object }, four: { five: 5, object }, five: 5 } var produce = immer.default
Tests:
immer
data = produce(INITIAL_DATA, draft => { draft.one.two.three.four.five = 0; })
shallow copy
data = { one: { two: { three: { four: { five: 0, ...INITIAL_DATA.one.two.three.four }, ...INITIAL_DATA.one.two.three }, ...INITIAL_DATA.one.two }, ...INITIAL_DATA.one }, ...INITIAL_DATA }
ramda lens
data = set(lensPath(['one', 'two', 'three', 'four', 'five']), 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 its test cases. **What is tested:** The provided benchmark compares three different approaches to update nested structures in JavaScript: 1. **Immer**: A library that provides an immutable data structure, allowing for efficient updates without mutating the original object. 2. **Shallow copy**: A basic approach that creates a new object by copying the existing one, which can lead to performance issues with deep structures. 3. **Ramda lens**: A library that provides a functional programming style for updating nested objects using a path-based API. **Options compared:** The benchmark compares the performance of these three approaches on a complex nested structure: * `immer`: updates the original object using the `immer` library's `produce` function. * `shallow copy`: creates a new object by copying the existing one, without mutating the original. * `ramda lens`: uses Ramda's `lensPath` and `set` functions to update specific parts of the nested structure. **Pros and cons:** 1. **Immer**: Pros: * Immutable data structures can provide thread-safety guarantees. * Immutability can make code easier to reason about and debug. 2 Cons: * Can introduce additional overhead due to object creation and cloning. * May require more memory allocation for large datasets. 3. **Shallow copy**: Pros: * Simple and lightweight implementation. 4. Cons: * Can lead to performance issues with deep structures, as new objects are created recursively. 5. **Ramda lens**: Pros: * Provides a functional programming style for updating nested objects. * Can be more concise and expressive than traditional imperative approaches. Cons: * May require additional memory allocation due to the creation of intermediate objects. **Library usage:** The benchmark uses two external libraries: 1. **Immer**: Used for creating an immutable data structure and providing efficient updates. 2. **Ramda**: Used for its `lensPath` and `set` functions, which provide a functional programming style for updating nested objects. **Special JS features or syntax:** No special JavaScript features or syntax are used in this benchmark. **Alternative approaches:** Other alternatives to these approaches include: 1. **ES6 spread operator**: Using the spread operator (`{ ...obj }`) to create a shallow copy of an object. 2. **Object.assign()**: Using `Object.assign()` to create a shallow copy of an object. 3. **Prototype-based inheritance**: Using prototype-based inheritance to avoid creating new objects and instead update existing ones. These alternatives have their own trade-offs in terms of performance, memory usage, and code readability. The choice of approach ultimately depends on the specific requirements and constraints of the project.
Related benchmarks:
Immer (setAutoFreeze(false)) vs shallow vs ramda lens vs immutabe js vs immutable js (toJS) vs mutating 2.0
Immer vs shallow vs ramda lens (2)
Immer vs shallow vs ramda assocPath (nested structure)
Immer vs shallow vs ramda assocPath (nested structure light)
Comments
Confirm delete:
Do you really want to delete benchmark?