Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Testing lodash get vs native javascript functions
(version: 0)
Comparing performance of:
get property using native JS vs get property using lodash
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
Script Preparation code:
const dataItem = { productId: 10047, productName: 'Chai', category : { categoryName: 'Beverages' } }; function getProperty(propertyName) { const parts = propertyName.split('.'); let property = dataItem; parts.forEach((propName) => property = property[propName]); return property; } function getPropertyUsingLodash(propertyName) { return _.get(dataItem, propertyName); }
Tests:
get property using native JS
var value = getProperty("category.categoryName");
get property using lodash
var value = getPropertyUsingLodash("category.categoryName");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
get property using native JS
get property using 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):
I'll break down the benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The test compares two approaches to access nested properties in an object: native JavaScript and Lodash's `_.get()` function. **Native JavaScript Approach** In the native JavaScript approach, a custom function `getProperty()` is defined to traverse the nested object structure. The function splits the property path into individual parts and iterates through each part to reach the final desired property value. Pros: 1. **Performance**: Native code can be optimized for performance, which might lead to better execution times. 2. **Control**: Developers have full control over the implementation, allowing for custom optimizations or features. Cons: 1. **Verbosity**: The native approach requires more manual effort and complexity in writing the property access function. 2. **Error Handling**: Without a library like Lodash, developers must handle potential errors (e.g., undefined properties) themselves. **Lodash `.get()` Function Approach** The Lodash `.get()` function is used to access nested properties in an object. This approach provides a concise and expressive way to traverse nested structures. Pros: 1. **Conciseness**: The `.get()` function reduces the amount of code needed to achieve the same result. 2. **Error Handling**: Lodash's `.get()` function can handle potential errors, such as undefined properties or arrays with non-numeric indices. Cons: 1. **Performance Overhead**: Using a library like Lodash might introduce additional overhead due to function calls and object lookups. 2. **Dependency**: The benchmark relies on the Lodash library being included in the test environment. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for tasks such as string manipulation, array operations, and object manipulation. The `.get()` function is one of its most useful tools for accessing nested properties in objects. **Special JS Feature/Syntax** None mentioned in this benchmark. **Other Alternatives** While Lodash's `.get()` function is a convenient solution, other libraries or approaches could be used: 1. **Array.prototype.at()**: Introduced in ECMAScript 2020, `at()` provides a way to access nested properties using dot notation. 2. **Object.entries() and Array.prototype[Symbol.iterator]**: These methods allow for iterating over object entries and array elements, enabling similar functionality to Lodash's `.get()` function. However, these alternatives might not be as well-established or widely supported as Lodash at this point in time. **Benchmark Considerations** When preparing benchmarks like this one, consider the following factors: 1. **Relevance**: Ensure that the test cases and benchmark definitions accurately reflect real-world scenarios. 2. **Consistency**: Use consistent naming conventions, code formatting, and testing frameworks to facilitate comparison and analysis of results. 3. **Data Quality**: Collect meaningful data from a diverse range of devices, browsers, and environments to ensure the benchmark is representative. By considering these factors, you can create a well-rounded benchmark that provides valuable insights into performance differences between native JavaScript and Lodash's `.get()` function.
Related benchmarks:
lodash._get vs Property dot notation
Lodash test suite
Lodash.get vs Lodash.property vs native vs native with optional chaining
deeply nested Lodash.get vs Lodash.property vs native
Comments
Confirm delete:
Do you really want to delete benchmark?