Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
imperative-prop-selector4
(version: 0)
Comparing performance of:
Imperative path vs Ramda path
Created:
7 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>
Script Preparation code:
var mock = { name: 'José Manuel', surname: 'Lucas', organization: { id: 11, selfcare: { id: 12355, customization: { colors: { foreground: '#fff', background: { primary: '#ff0000', } } } } }, }; const getName = ({ name }) => name; const getOrganization = obj => obj && obj.organization; const getSelfcare = (obj) => { const a = getOrganization(obj); return a && a.selfcare; }; const getCustomization = (obj) => { const a = getSelfcare(obj); return a && a.customization; }; const getColors = (obj) => { const a = getCustomization(obj); return a && a.colors; }; var imperativePath = (obj) => { const a = getColors(obj); return a && a.foreground; }; const { pipe, prop, defaultTo } = R; const organizationSelector = prop('organization'); const selfcareSelector = pipe( organizationSelector, prop('selfcare'), ); const customizationSelector = pipe( selfcareSelector, prop('customization'), ); const colorsSelector = pipe( customizationSelector, prop('colors'), ); var ramdaPath = pipe( colorsSelector, prop('foreground'), ); // Ensuring that all functions work properly if (ramdaPath(mock) !== '#fff') { throw new Error('Invalid ramda path'); } if (imperativePath(mock) !== '#fff') { throw new Error('Invalid imperative path'); }
Tests:
Imperative path
imperativePath(mock);
Ramda path
ramdaPath(mock);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Imperative path
Ramda path
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 explain what is being tested. **Benchmark Overview** The benchmark is designed to compare two approaches to select a specific value from an object: imperative (using a simple chain of functions) and Ramda's `pipe` function. **Imperative Path** The imperative path uses a series of individual functions to navigate the object: 1. `getOrganization(obj)` returns the organization object. 2. `getSelfcare(a)` returns the selfcare object if `a` is not null or undefined. 3. `getCustomization(b)` returns the customization object if `b` is not null or undefined. 4. `getColors(c)` returns the colors object if `c` is not null or undefined. 5. `imperativePath(obj)` returns the foreground color if `obj` has a colors object and the foreground property exists. The entire chain of functions is executed using the `imperativePath(mock)` call. **Ramda Path** The Ramda path uses the `pipe` function to compose multiple functions together: 1. `organizationSelector(prop('organization'))`: returns the organization object. 2. `selfcareSelector(organizationSelector)`: returns the selfcare object if it exists in the organization. 3. `customizationSelector(selfcareSelector)`: returns the customization object if it exists in the selfcare. 4. `colorsSelector(customizationSelector)`: returns the colors object if it exists in the customization. 5. `prop('foreground')`: returns the foreground color. The entire chain of functions is executed using the `ramdaPath(mock)` call. **Comparison** The benchmark measures the execution time of both approaches to retrieve the foreground color from the mock object. **Pros and Cons** * **Imperative Path**: Pros: + Easy to understand for those familiar with imperative programming. + May be more efficient for small objects. Cons: + Can lead to deep function calls, which can be slower due to overhead. + Requires explicit error handling (e.g., `getSelfcare(a)` returns null). * **Ramda Path**: Pros: + Composes functions in a declarative way, making the code more concise and easier to read. + Reduces overhead by executing multiple functions in a single call. Cons: + May be less intuitive for those unfamiliar with functional programming. + Requires additional dependencies (e.g., Ramda library). **Other Considerations** * **Memory Usage**: Both approaches have similar memory usage, as they both access the same properties of the object. **Library and Special JS Feature** The benchmark uses the Ramda library, which provides a set of functional programming utilities. The `pipe` function is used to compose multiple functions together in a declarative way. Note that there are no special JavaScript features or syntaxes used in this benchmark.
Related benchmarks:
test object copy vs adding properties
Object.assign vs direct assign
array.push vs object literal vs array literal
array.push array literal
hasOwnProperty vs Object.entries
Comments
Confirm delete:
Do you really want to delete benchmark?