Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
R.pathOr vs optional chaining
(version: 0)
Comparing performance of:
With Ramda vs With optional chaining
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.30.0/ramda.min.js"></script>
Script Preparation code:
var account1 = { subscriptions: { suite: { status: 'in_trial', currentTermStart: new Date(), currentTermEnd: new Date() } } }; var account2 = { subscriptions: { suite: { status: 'active', currentTermStart: new Date(), currentTermEnd: new Date() } } }; var account3 = { subscriptions: { myplan_v1: { status: 'active', currentTermStart: new Date(), currentTermEnd: new Date() } } }; var iterations = 1000000;
Tests:
With Ramda
for (i = 0; i < iterations; i++) { const res1 = R.pathOr(null, ['subscriptions', 'suite', 'status'], account1) === 'in_trial'; const res2 = R.pathOr(null, ['subscriptions', 'suite', 'status'], account2) === 'in_trial'; const res3 = R.pathOr(null, ['subscriptions', 'suite', 'status'], account3) === 'in_trial'; }
With optional chaining
for (i = 0; i < iterations; i++) { const res1 = account1?.subscriptions?.suite?.status === 'in_trial'; const res2 = account2?.subscriptions?.suite?.status === 'in_trial'; const res3 = account3?.subscriptions?.suite?.status === 'in_trial'; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
With Ramda
With optional chaining
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:144.0) Gecko/20100101 Firefox/144.0
Browser/OS:
Firefox 144 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
With Ramda
10.7 Ops/sec
With optional chaining
297.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain the benchmark test and its options. **Benchmark Test** The benchmark test compares two ways to access nested object properties in JavaScript: using `R.pathOr` from the Ramda library (a functional programming library) and the optional chaining operator (`?.`) introduced in ECMAScript 2020. **Options Compared** 1. **Ramda's `pathOr`**: This function takes three arguments: a value to default to if the path doesn't exist, an array of keys to access the nested property, and an object to search for the property. 2. **Optional Chaining Operator (`?.`)**: This operator is used to access nested properties in a more concise way. It returns `undefined` if any part of the expression is `null` or `undefined`. **Pros and Cons** 1. **Ramda's `pathOr`**: * Pros: + More explicit and predictable way to handle missing keys. + Can be useful for complex path expressions. * Cons: + Requires importing an additional library (Ramda). + May have performance overhead due to the additional function call. 2. **Optional Chaining Operator (`?.`)**: * Pros: + More concise and readable way to access nested properties. + Built-in operator, so no additional imports are needed. * Cons: + Less explicit and may lead to unexpected behavior if not used carefully. **Other Considerations** 1. **Performance**: In general, the optional chaining operator (`?.`) is likely to be faster than using `pathOr` due to its built-in nature and fewer function calls. 2. **Code Readability**: The optional chaining operator (`?.`) is generally more concise and readable than using `pathOr`, especially for simple path expressions. **Library: Ramda** Ramda is a functional programming library that provides a set of higher-order functions for working with arrays, objects, and other data structures. In this benchmark test, `R.pathOr` is used to access nested properties in a more explicit way. **Special JS Feature/ Syntax** The optional chaining operator (`?.`) was introduced in ECMAScript 2020 as part of the `Object.entries()` method. It's a concise way to access nested properties and can be used with any object, not just objects that inherit from `Object`. Other alternatives for this benchmark test could include: * Using other libraries or frameworks that provide similar functionality to Ramda, such as Lodash. * Implementing custom functions for accessing nested properties using loops or recursive functions. * Using other ES6+ features, such as destructuring or template literals, to access nested properties.
Related benchmarks:
Optional Chaining versus _.get lodash
Optional Chaining versus _.get lodash
Optional Chaining versus _.get lodash (with obj in the optional chain test)
Optional Chaining versus _.get lodash aaaaa
Optional chaining vs native code v3
Comments
Confirm delete:
Do you really want to delete benchmark?