Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Random Getters
(version: 4)
Comparing performance of:
Lodash vs Mine vs Mine2 vs Mine3
Created:
6 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js'></script>
Script Preparation code:
var obj = { a: { b: { c: { foo: { bar() { return '42' } } } } } } function getProp1(list, obj) { const selector = list.split('.') if (arguments.length === 1) return _obj => getProp1(list, _obj) for (const prop of selector) { if (obj[prop]) { obj = obj[prop] } } return obj } function getProp2(list, obj) { const selector = list.split('.') const lengthof = selector.length if (arguments.length === 1) return _obj => getProp2(list, _obj) for (const prop of selector) { if (obj[prop]) { obj = obj[prop] } } for (let i = 0; i < lengthof; i++) { if (obj[selector[i]]) { obj = obj[prop] } } return obj } function getProp3(list, obj) { const selector = list.split('.') if (arguments.length === 1) return _obj => getProp3(list, _obj) for (const i in selector) { if (obj[selector[i]]) { obj = obj[selector[i]] } } return obj } function path(list, obj){ if (arguments.length === 1) return _obj => path(list, _obj) if (obj === null || obj === undefined){ return undefined } let willReturn = obj let counter = 0 const pathArrValue = typeof list === 'string' ? list.split('.') : list while (counter < pathArrValue.length){ if (willReturn === null || willReturn === undefined){ return undefined } willReturn = willReturn[ pathArrValue[ counter ] ] counter++ } return willReturn }
Tests:
Lodash
_.get('a.b.c.foo.bar', obj)
Mine
getProp1('a.b.c.foo.bar')(obj)
Mine2
getProp2('a.b.c.foo.bar')(obj)
Mine3
getProp3('a.b.c.foo.bar')(obj)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash
Mine
Mine2
Mine3
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's being tested. **Benchmark Description** The benchmark tests how to access nested properties in an object using different approaches. The test case uses a JSON object with several levels of nesting, represented by the string "a.b.c.foo.bar". **Approaches Compared** There are four approaches compared: 1. **Lodash (`_.get('a.b.c.foo.bar', obj)`)**: Lodash is a popular JavaScript library that provides a function `get()` to access nested properties in an object. 2. **`getProp1('a.b.c.foo.bar')(obj)`**: This approach uses a custom function `getProp1()` that splits the property path into an array and iterates over it, accessing each property in turn. 3. **`getProp2('a.b.c.foo.bar')(obj)`**: Similar to `getProp1()`, but with a different iteration logic. 4. **`getProp3('a.b.c.foo.bar')(obj)`**: This approach uses another custom function `getProp3()` that iterates over the property path using `for...in`. **Pros and Cons** * **Lodash (`_.get()`)**: Pros: + Concise and readable code. + Fast and efficient, thanks to Lodash's optimized implementation. * Cons: + Adds external dependency (Lodash library). * **`getProp1()`**, **`getProp2()`,** and **`getProp3()`**: Pros: + No external dependencies. + Can be customized for specific use cases. * Cons: + Code can be more verbose and harder to read. + May be slower than Lodash's implementation due to custom iteration logic. **Other Considerations** * The benchmark measures the execution speed of each approach, which is influenced by factors like JavaScript engine optimizations, caching, and the specific hardware platform. * The use of a large object with many nested properties makes it harder for the benchmark to measure performance differences between approaches. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for common tasks, such as array manipulation, string formatting, and more. In this case, the `get()` function is used to access nested properties in an object. The `_.get()` function takes two arguments: the property path (e.g., "a.b.c.foo.bar") and the object to access (in this case, `obj`). It returns the value of the property at that path. **Special JS Feature/Syntax** None mentioned.
Related benchmarks:
loop count
loop count
Object copy loop compare
or vs some
or vs some 2
Comments
Confirm delete:
Do you really want to delete benchmark?