Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.get vs safeGet vs Native
(version: 0)
Comparing performance of:
Lodash get vs safeGet vs Native
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:
var person = {name: 'Frederick', lastName: 'Corcino Alejo', nested: {name: 'test'}}; function safeGet(obj, path, defaultValue) { return path.split('.').reduce((xs, x) => (xs && xs[x]) ? xs[x] : defaultValue, obj); }
Tests:
Lodash get
_.get(person, 'name', ''); _.get(person, 'nested.name', '');
safeGet
safeGet(person, 'name', ''); safeGet(person, 'nested.name', '');
Native
person.name; person.nested.name;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash get
safeGet
Native
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:136.0) Gecko/20100101 Firefox/136.0
Browser/OS:
Firefox 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash get
2432584.8 Ops/sec
safeGet
3013261.0 Ops/sec
Native
680453248.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark. **Benchmark Purpose:** The goal of this benchmark is to compare the performance of three approaches: 1. `Lodash.get`: A function from the popular Lodash library that allows safe and efficient access to nested object properties. 2. `safeGet`: A custom implementation of a safe getter function, which also accesses nested object properties. 3. Native code: Directly accessing object properties using the dot notation (`person.name`). **Options Compared:** The benchmark compares the performance of these three approaches: * `Lodash.get` * `safeGet` * Native code **Pros and Cons:** 1. **Lodash.get**: Pros: * High-level abstraction, making it easier to write code that accesses nested properties. * Provides a safe way to avoid " Property X does not exist" errors. Cons: * Adds overhead due to the need to import and initialize the Lodash library. 2. **safeGet**: Pros: + Lightweight implementation with minimal overhead. Cons: + Requires manual error handling for cases where the property path is invalid or non-existent. 3. **Native code**: Pros: + No additional overhead from libraries or function calls. Cons: + Error-prone, as it can lead to "Property X does not exist" errors if the property name is incorrect. **Library:** In this benchmark, Lodash is used to provide a safe and efficient way to access nested object properties. The `Lodash.get` function is specifically designed for this purpose and provides a convenient abstraction over accessing nested properties. **JavaScript Features/Syntax:** This benchmark uses JavaScript's dot notation (`person.name`) to access object properties directly in the native code test case. This syntax is widely supported in modern browsers and environments. **Other Considerations:** * Performance: The benchmark focuses on measuring execution speed, but other factors like memory usage or garbage collection might be relevant depending on the specific use case. * Error handling: The `safeGet` implementation requires manual error handling, while `Lodash.get` provides a safe way to avoid errors. Native code relies on JavaScript's built-in error handling mechanisms. **Alternatives:** Other alternatives to consider when accessing nested object properties include: 1. **Using bracket notation (`person['name']`) instead of dot notation**: This can provide better performance in some cases, as it avoids the overhead of function calls and property lookups. 2. **Using a library like `Object.getPrototypeOf()` or `JSON.parse()`**: These functions can be used to access nested properties in different ways, but might not offer the same convenience as Lodash's `get` method. 3. **Implementing custom getters and setters for objects**: This approach provides more fine-grained control over property access, but requires additional boilerplate code. In summary, this benchmark provides a useful comparison of three approaches to accessing nested object properties in JavaScript, highlighting the trade-offs between convenience, safety, and performance.
Related benchmarks:
Comparing performance of: Lodash get vs Native with object checking
Lodash.get vs safeGet vs Native vs Native?
Lodash.get vs Property dot notation @movlan
Lodash.get vs Property dot notation with longer path
Comments
Confirm delete:
Do you really want to delete benchmark?