Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash prop VS JS pure prop
(version: 0)
Comparing performance of:
JS pure vs Lodash
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
Script Preparation code:
var a = {b: {c: {d: {e: [null, null, null, {f: 125}]}}}};
Tests:
JS pure
function prop(reference, target) { let targetSplited = target.split('.'), result = reference; for (let i = 0, l = targetSplited.length; i < l; i++) { let targetCurrent = targetSplited[i], resultTarget = result[targetCurrent]; if (resultTarget) { result = resultTarget; } else { result = null; break; }; } return result; } var result = prop(a, 'b.c.d.e.3.f')
Lodash
var result = _.property('b.c.d.e.3.f')(a)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
JS pure
Lodash
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 provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches to accessing nested properties in JavaScript objects: a vanilla (pure) implementation using a loop, and an implementation from the Lodash library. **Script Preparation Code** The script preparation code creates an object `a` with nested properties, specifically: ```javascript var a = { b: { c: { d: { e: [null, null, null, { f: 125 }] }} } }; ``` This creates a hierarchical structure with multiple levels of nesting. **HTML Preparation Code** The HTML preparation code includes the Lodash library (version 4.17.11) via a CDN: ```html <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script> ``` This is not directly relevant to the benchmark, but it's essential for executing the Lodash implementation. **Benchmark Definition** The benchmark definition consists of two test cases: 1. **JS pure**: This implementation uses a loop to access nested properties: ```javascript function prop(reference, target) { let targetSplited = target.split('.'), result = reference; for (let i = 0, l = targetSplited.length; i < l; i++) { let targetCurrent = targetSplited[i], resultTarget = result[targetCurrent]; if (resultTarget) { result = resultTarget; } else { result = null; break; } } return result; } var result = prop(a, 'b.c.d.e.3.f'); ``` This implementation assumes that the target property exists at every level of nesting. 2. **Lodash**: This implementation uses the `_.property` function from Lodash to access nested properties: ```javascript var result = _.property('b.c.d.e.3.f')(a); ``` **Pros and Cons** **JS pure:** Pros: * Pure JavaScript, no external library dependencies. * Can be optimized for specific use cases. Cons: * More complex implementation, potentially slower due to the loop. * May not work correctly if the target property does not exist at every level of nesting. **Lodash:** Pros: * Optimized for performance and correctness. * Uses a well-known and widely-used library (Lodash). Cons: * Additional library dependency, which may impact performance. * Less flexible than the pure JavaScript implementation. **Library Consideration** The Lodash library is used to provide an optimized solution for accessing nested properties. The `_.property` function is designed to handle common use cases, but it's not a general-purpose property access function. **Special JS Feature/Syntax** None of the implementations rely on any special JavaScript features or syntax beyond standard ECMAScript 6+ features. **Other Alternatives** If you wanted to implement this benchmark without Lodash, you could consider using other libraries or frameworks that provide optimized property access functionality, such as: * `Object.prototype.hasOwnProperty.call` and chaining for deeper nesting. * `Array.prototype.includes` with string interpolation for nested property access. * Custom implementations using bit manipulation or other techniques. Keep in mind that these alternatives may not be as performant or convenient as the Lodash implementation.
Related benchmarks:
lodash.keys vs Object.keys
lodash.keys vs Object.keys
lodash.keys vs Object.keys
lodash.keys vs Object.keys
isEmpty vs Object.keys
Comments
Confirm delete:
Do you really want to delete benchmark?