Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer vs shallow vs ramda assocPath (nested structure light)
(version: 0)
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.four.five = 0; })
shallow copy
data = { four: { five: 0, ...INITIAL_DATA.four }, ...INITIAL_DATA }
ramda assocPath
data = assocPath(['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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
immer
94058.7 Ops/sec
shallow copy
2627354.2 Ops/sec
ramda assocPath
2239383.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark measures the performance of three different approaches to update a nested object: 1. **Immer**: A library that provides a simple way to create immutable objects by creating a new copy of an existing object. 2. **Shallow Copy**: A basic approach where a new object is created and its properties are copied from the original object using object spread (`{ ...INITIAL_DATA }`). 3. **Ramda assocPath**: A function from the Ramda library that allows you to access nested objects by specifying their path. **Benchmark Test Cases** The test cases compare the performance of each approach: * **Immer**: The benchmark uses Immer's `produce` function to create a new copy of the initial data with a specific update. * **Shallow Copy**: A basic object spread is used to create a shallow copy of the initial data with an updated value in one of its properties. * **Ramda assocPath**: This approach uses Ramda's `assocPath` function to access and update a nested object by specifying its path. **Pros and Cons** Here's a brief summary of each approach: 1. **Immer**: * Pros: Simple, intuitive API, efficient for large objects. * Cons: Creates an entirely new copy of the original data, which can be memory-intensive. 2. **Shallow Copy**: * Pros: Lightweight, easy to implement, suitable for small objects or updates. * Cons: Updates are shallow (only affects top-level properties), may not preserve nested object relationships. 3. **Ramda assocPath**: * Pros: Flexible, allows for precise control over path updates, efficient for large objects. * Cons: Requires familiarity with Ramda's API, may be slower due to the function call overhead. **Library and Syntax** The benchmark uses: * **Immer**: A JavaScript library that provides immutable data structures. The `produce` function creates a new copy of an object by specifying an update function. * **Ramda**: A functional programming library that provides various utility functions, including `assocPath`, which allows you to access nested objects. No special JS features or syntax are used in this benchmark. **Alternatives** For large-scale data updates, alternative approaches like: * **Propt**: A lightweight library that creates a shallow copy of an object while preserving existing properties. * **Object.assign()`: Another way to create a shallow copy using the `Object.assign()` method. * **Native array methods**: For updating arrays, you can use `slice()` and then assign the updated values. These alternatives might not be as efficient or flexible as Immer's `produce` function or Ramda's `assocPath`, but they can be suitable for specific use cases.
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)
Comments
Confirm delete:
Do you really want to delete benchmark?