Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ramda-path-vs-lodash-get-vs-custom-deepak
(version: 0)
Comparing performance of:
ramda path vs lodash get vs custom get
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/get.min.js"></script> <script src="//cdn.jsdelivr.net/npm/ramda@latest/dist/ramda.min.js"></script>
Script Preparation code:
var obj = {foo: {bar: {tar: [1,2,3]}}}; function _get(obj, path, defaultValue = undefined) { const fullPath = path.replace(/\[/g, '.').replace(/]/g, '').split('.').filter(Boolean); return fullPath.every(everyFunc) ? obj : defaultValue; function everyFunc(step) { return !(step && (obj = obj[step]) === undefined); } }
Tests:
ramda path
R.path(['foo', 'bar', 'tar', 2], obj)
lodash get
_.get(obj, 'foo.bar.tar.2')
custom get
_get(obj, 'foo.bar.tar.2')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
ramda path
lodash get
custom get
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
ramda path
5370616.5 Ops/sec
lodash get
6107495.0 Ops/sec
custom get
1935933.6 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. **Benchmark Overview** The benchmark compares three approaches to access a nested object property: Ramda's `path` function, Lodash's `_get` function, and a custom implementation (`_get`). **What's Being Tested** Each test case measures the execution time of accessing a specific nested object property using each of the three approaches. The properties being accessed are: 1. `foo.bar.tar[2]` 2. `foo.bar.tar[3]` The tests are run on the same input data, which is an object with a nested structure. **Options Compared** Here's a brief overview of each approach and its pros/cons: 1. **Ramda's `path` function**: Pros: * Lightweight and efficient. * Well-documented and widely used in the functional programming community. Cons: * Can be less readable for those unfamiliar with functional programming concepts. 2. **Lodash's `_get` function**: Pros: * Well-maintained and widely used library. * Supports various access modes (e.g., default value, strict mode). Cons: * Larger bundle size compared to Ramda's `path` function. 3. **Custom implementation (`_get`)**: Pros: * Can be optimized for specific use cases or performance profiles. Cons: * Requires more code and maintenance effort. **Library Used** The custom implementation uses a simple recursive approach to access the nested object property. It splits the path string into individual keys and iterates through them, using the `obj` variable as the current context. ```javascript function _get(obj, path, defaultValue = undefined) { const fullPath = path.replace(/\\[/g, '.').replace(/]/g, '').split('.').filter(Boolean); return fullPath.every(everyFunc) ? obj : defaultValue; function everyFunc(step) { return !(step && (obj = obj[step]) === undefined); } } ``` **Special JS Feature or Syntax** None are mentioned in this benchmark. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * **Array.prototype.at()**: This method is introduced in ECMAScript 2020 and provides a more concise way to access nested object properties. However, it may not be supported by older browsers. * **Object.entries() and array reduction**: Another approach could involve using `Object.entries()` to iterate over the object's key-value pairs and then reducing the resulting array to access the desired property. Keep in mind that these alternatives might have different performance characteristics or trade-offs compared to Ramda's `path` function, Lodash's `_get` function, or the custom implementation.
Related benchmarks:
hoek vs lodash
ramda-path-vs-lodash-get
get vs path
lodash.get vs optional chaining vs safeGet
Comments
Confirm delete:
Do you really want to delete benchmark?