Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_,get vs get
(version: 0)
Comparing performance of:
_.get vs native
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Tests:
_.get
var object = { 'a': [{ 'b': { 'c': 3 } }] }; _.get(object, 'a[0].b.c');
native
var object = { 'a': [{ 'b': { 'c': 3 } }] }; const get = (obj, path, defaultValue) => { const result = String.prototype.split.call(path, /[,[\].]+?/) .filter(Boolean) .reduce((res, key) => (res !== null && res !== undefined) ? res[key] : res, obj); return (result === undefined || result === obj) ? defaultValue : result; } get(object, 'a[0].b.c');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.get
native
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/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.get
4709853.0 Ops/sec
native
1676316.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The test creates an object `object` with nested properties: `{ 'a': [{ 'b': { 'c': 3 } }] }`. The goal is to retrieve the value of `object.a[0].b.c`. **Options Compared** Two options are compared: 1. **Lodash's `_get()` function**: This is a utility function from the Lodash library that takes an object, a path (e.g., `'a[0].b.c'`), and returns the value at that path if it exists, or a default value otherwise. 2. **Native implementation**: A custom implementation of the same logic using JavaScript's `String.prototype.split()` method to extract the path components and then recursively traversing the object to find the value. **Pros and Cons** ### Lodash's `_get()` function Pros: * Lightweight and efficient, as it's a specialized utility function designed for this specific use case. * Easy to read and maintain, with a clear and concise API. Cons: * Requires an external library (Lodash) to be included in the test environment. * May not be suitable for all scenarios where custom logic is required or performance optimization is necessary. ### Native implementation Pros: * No external dependencies, as it's implemented directly in JavaScript. * Can be optimized for specific use cases or performance requirements. Cons: * More complex and harder to read due to the recursive function definition. * May incur additional overhead due to the custom logic. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a collection of reusable functions, including `_get()`. It's designed to make common tasks easier and more efficient. In this benchmark, Lodash's `_get()` function is used to retrieve values from nested objects. **Special JS Feature or Syntax** None mentioned in the provided code snippet. **Other Alternatives** If you were to implement a custom solution without using an external library, you might consider using other approaches: * Using a recursive function with a stack-based implementation. * Utilizing `JSON.parse()` and `JSON.stringify()` methods to serialize the object path as a JSON string. * Leveraging built-in functions like `Object.keys()`, `Array.prototype.forEach()`, or `String.prototype.split()`. Keep in mind that these alternatives might not be as efficient or readable as the native implementation or Lodash's `_get()` function.
Related benchmarks:
es6 destructuring vs lodash _.get asjfoasijdfio
isEmpty vs. vanilla
Direct object access vs _.get
lodash get vs es6
Lodash.get vs Property dot notation vs Own get coded manually
Comments
Confirm delete:
Do you really want to delete benchmark?