Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer vs shallow vs ramda assocPath (nested structure)
(version: 1)
Comparing performance of:
immer vs shallow copy vs ramda assocPath
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 { assocPath } = 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 assocPath
data = assocPath(['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 assocPath
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 what's being tested in the provided benchmark. **Benchmark Overview** The benchmark compares three different approaches to modify nested structures in JavaScript: 1. `Immer` 2. Shallow copy (a manual implementation) 3. Ramda's `assocPath` **Options Being Compared** * **Immer**: Immer is a library that provides a simple and efficient way to create immutable data structures. It allows you to create a shallow copy of an object by creating a new draft object and then updating the original object with the changes. * **Shallow Copy**: This is a manual implementation of creating a shallow copy of an object. It involves using the `Object.assign()` method or the spread operator (`{...}`) to create a new object and then copying the properties from the original object into the new one. * **Ramda's assocPath**: Ramda's `assocPath` is a function that allows you to update a nested structure by providing an array of keys to navigate to. It returns a new value with the updated property. **Pros and Cons** * **Immer**: + Pros: Efficient, easy to use, and well-maintained. + Cons: Only creates shallow copies; may not be suitable for deep or complex structures. * **Shallow Copy**: + Pros: Manual control over creation of a new object; can be used in situations where Immer is not suitable. + Cons: May be slower than Immer, and requires manual effort to create the new object. * **Ramda's assocPath**: + Pros: Provides fine-grained control over updating nested structures; can be useful for complex updates. + Cons: May be slower than Immer or shallow copy; has a steeper learning curve due to its functional programming style. **Library and Syntax** * **Immer**: Immer is a JavaScript library that provides a simple and efficient way to create immutable data structures. It uses a `produce` function to create a new draft object and then updates the original object with the changes. * **Ramda's assocPath**: Ramda is a functional programming library that provides various utility functions, including `assocPath`. This function takes an array of keys and a value as arguments and returns a new value with the updated property. **Benchmark Test Cases** Each test case uses a different approach to modify the nested structure: * **Immer**: Uses the `immer.default` produce function to create a new draft object and update the original object. * **Shallow Copy**: Manually creates a new object using `Object.assign()` or the spread operator (`{...}`) and then copies properties from the original object into the new one. * **Ramda's assocPath**: Uses the `assocPath` function to navigate to specific keys in the nested structure and update the value. **Other Alternatives** Some other alternatives for modifying nested structures in JavaScript include: * Using the `Object.assign()` method with a deep copy option * Using the `lodash` library, which provides various utility functions for working with objects and arrays * Implementing a custom solution using a combination of recursive functions or functional programming techniques
Related benchmarks:
Immer vs shallow copy222ddd
Immer vs shallow vs ramda lens (2)
Immer vs shallow vs ramda lens (nested structure)
Immer vs shallow vs ramda assocPath (nested structure light)
Comments
Confirm delete:
Do you really want to delete benchmark?