Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lens vs iu-object
(version: 0)
Comparing perf of ramda lens vs https://github.com/attack-monkey/immutable-update
Comparing performance of:
Spread vs Ramda lens vs IU
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
Script Preparation code:
// Initial state var state = { visible: false, lazy: { test: true, number: 44 }, count: 1 } // Define reducers ahead of time var spread = state => ({ ...state, lazy: { ...state.lazy, test: !state.lazy.test } }) var lens = R.lensPath(["lazy", "test"]) var ramda = state => { return R.over(lens, test => !test, state) } var iuUpdate = state => iu(state, "lazy/test", !state.lazy.test) function iu(state, subArray, val) { if (!subArray) { console.log("No target provided") } else if (subArray.length < 1) { console.log("No target provided") } else if (!state) { console.log("No state object provided") } else { if (typeof subArray === "string") { subArray = subArray.split("/").filter(segment => segment) } return reducer(state, subArray, cloner(val), 0) } } function reducer(_state, subArray, val, l) { try { const key = subArray[l] if (l + 1 === subArray.length) { const replacer = isEmpty(val) ? undefined : val return replacer !== undefined ? Object.assign({}, _state, { [key]: replacer }) : stateWithoutKey(_state, key) } else { const value = !isEmpty(_state[key]) ? _state[key] : {} return Object.assign({}, _state, { [key]: reducer(value, subArray, val, l + 1), }) } } catch (error) { console.log("reducer error =>", error) } } function cloner(arg) { try { return JSON.parse(JSON.stringify(arg)) } catch (e) { return null } } function isEmpty(val) { return ( val === undefined || val === null || (typeof val === "object" && Array.isArray(val) && val.length === 0) || (typeof val === "object" && !Array.isArray(val) && Object.keys(val).length === 0) ) } function stateWithoutKey(state, key) { const { [key]: value, ...withoutKey } = state return withoutKey }
Tests:
Spread
var data = spread(state)
Ramda lens
var data = ramda(state)
IU
var data = iuUpdate(state)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Spread
Ramda lens
IU
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 this benchmark. **Benchmark Definition** The benchmark is comparing the performance of three different approaches to update an object: 1. **Lens**: A functional programming library that provides a way to create and manipulate lenses, which are functions that focus on specific parts of an object. 2. **IU (Immutable Update)**: An implementation of immutable updates, which ensures that data is never mutated in place. Instead, new objects are created with the updated values. 3. **Ramda lens**: A variant of the Lens library that uses Ramda, a functional programming library. **Benchmark Preparation Code** The preparation code sets up an initial state object `state` and defines two functions: 1. `spread`: Spreads the state object into a new object using the spread operator (`{...state}`). 2. `ramda`: Uses the Lens library to create a lens that focuses on the `lazy.test` property of the `state` object, and then uses Ramda's `R.over` function to update the value of this property. **IU Update Implementation** The IU Update implementation is a custom function `iuUpdate` that takes an object `state` as input. It uses two functions: 1. `reducer`: Recursively updates the state object by creating new objects with updated values. 2. `cloner`: Creates a deep copy of an object using JSON parsing and stringification. **Options Compared** The benchmark is comparing the performance of these three approaches to update an object: * **Lens**: Uses a functional programming approach to create and manipulate lenses. * **Ramda lens**: A variant of Lens that uses Ramda for functional programming. * **IU (Immutable Update)**: Ensures data immutability by creating new objects with updated values. **Pros and Cons** Here are some pros and cons of each approach: * **Lens**: + Pros: Efficient use of function composition, flexible lens creation. + Cons: May require more code to set up and use lenses. * **Ramda lens**: Similar to Lens, but uses Ramda for functional programming. + Pros: Combines the benefits of Ramda with Lens. + Cons: May have slightly slower performance due to Ramda overhead. * **IU (Immutable Update)**: + Pros: Ensures data immutability, easy to implement recursive updates. + Cons: Creates new objects with updated values, potentially leading to higher memory usage. **Device Platform and Browser** The benchmark results are reported for a Chrome 69 browser on a Windows 7 desktop device. The executions per second (EPS) value indicates the number of updates performed per second. Overall, this benchmark provides a comparison of three different approaches to update an object in JavaScript, highlighting the pros and cons of each approach.
Related benchmarks:
Ramda Lens vs Patchinko vs Object spread
Immer (setAutoFreeze(false)) vs shallow vs ramda lens vs immutable js
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?