Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.js get vs Native custom function
(version: 0)
Comparing performance of:
Native vs Lodash.js get
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
var families = { "Trump": { "members": { "father": { "children": [{ "name": "Berta", "age": "18" }, { "name": "Collado", "age": "81" } ] } } } }; var getValueByPath = function(root) { return function(path, def) { var result = path.split('.').reduce(function(object, piece) { return (object || {})[piece]; }, root); if (result === undefined || result === null) { return def; } return result; }; };
Tests:
Native
getValueByPath(families)('Trump.members.father.children.0.name', 'Johan')
Lodash.js get
_.get(families, 'Trump.members.father.children[0]name', 'Johan');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Native
Lodash.js get
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 what's being tested in the provided JSON. **Benchmark Definition** The benchmark is comparing two approaches to retrieve a value from an object using JavaScript: 1. **Native custom function**: The `getValueByPath` function, which uses recursion to traverse the object path and returns a default value if the path is not found. 2. **Lodash.js get**: A built-in function provided by the popular Lodash library, which does the same thing but with a simpler implementation. **Options compared** The two options being compared are: * `getValueByPath` (native custom function) * `_get` from Lodash.js These functions have different implementations and use cases. Let's explore their pros and cons: **Native custom function (`getValueByPath`)** Pros: * Flexibility: Can handle any object structure and path. * Control: Provides more control over the execution flow. Cons: * Complexity: The recursive implementation can lead to stack overflow issues for deep object paths. * Performance: May be slower due to the overhead of recursion. **Lodash.js get** Pros: * Simplicity: A lightweight, optimized implementation that's easy to use and understand. * Performance: Generally faster than the native custom function, as it uses a more efficient algorithm. Cons: * Limited flexibility: Only works with Lodash.js versions that support the `_get` method (version 4.17.4 or later). * Dependence on external library: Requires including the Lodash.js library in the test environment. **Library: Lodash.js** Lodash.js is a popular JavaScript utility library developed by Isaac Schlueter. It provides a collection of functional programming helpers, including the `_get` method used in this benchmark. The `_get` method allows you to safely navigate nested objects and retrieve values using dot notation or bracket notation. **Special JS feature/syntax** There's no specific special JavaScript feature or syntax being tested in this benchmark. The focus is on comparing two implementation approaches for a specific use case. **Other alternatives** If you wanted to test alternative implementations, you could consider: * Using other libraries like jQuery or Ramda.js, which provide similar functionality. * Implementing the logic yourself using basic JavaScript constructs (e.g., loops, conditional statements). * Using a different data structure, such as an array of objects, and modifying the implementation accordingly. However, these alternatives might not be suitable for this specific benchmark, as they would likely change the fundamental nature of the comparison.
Related benchmarks:
Comparing performance of: Lodash get vs Native with object checking
optional chaining vs lodash get
Native Object.values().some() vs lodash _.some()
Lodash.get vs safeGet vs Native vs Native?
Lodash.get vs Property dot notation with longer path
Comments
Confirm delete:
Do you really want to delete benchmark?