Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash get vs native js
(version: 0)
Comparing performance of:
lodash vs native
Created:
5 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>
Script Preparation code:
function get(obj, path, defaultValue) { const keys = path.split("."); let value = obj; try { for (key of keys) { value = value[key]; } return (typeof value === "undefined") ? defaultValue : value; } catch (error) { return defaultValue; } }
Tests:
lodash
_.get({"a": {"b": {"c": {"d": {"e": 1} } } } }, "a.b.c.d.e", 0);
native
get({"a": {"b": {"c": {"d": {"e": 1} } } } }, "a.b.c.d.e", 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash
native
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash
8101849.5 Ops/sec
native
6056691.5 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 Description** The benchmark is comparing the performance of two approaches: using Lodash (a popular JavaScript utility library) and native JavaScript to access nested properties in an object. **Options Compared** There are two options being compared: 1. **Lodash**: Using the `_.get()` function from Lodash, which provides a convenient way to access nested properties in objects. 2. **Native JavaScript**: Using a simple, recursive approach to access nested properties in an object. **Pros and Cons of Each Approach** **Lodash:** Pros: * Concise and readable code * Reduces the risk of errors due to typos or incorrect property names * Provides a consistent and predictable way to access nested properties Cons: * Introduces additional overhead due to the library's existence and initialization * May not be optimized for performance, especially for very large objects **Native JavaScript:** Pros: * Lightweight and doesn't introduce any additional overhead * Can be optimized for performance by minimizing the number of iterations over the object's properties Cons: * Requires more boilerplate code to achieve the same result as Lodash * May lead to errors if the property names are incorrect or typos occur **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for various tasks, including array manipulation, object manipulation, and more. The `_.get()` function is specifically designed to access nested properties in objects. It takes three arguments: the object to search, the property path (a string or an array), and an optional default value. **Special JS Feature/ Syntax** None of the test cases use any special JavaScript features or syntax that would affect their interpretation. **Other Alternatives** If you want to compare performance between Lodash and native JavaScript approaches for accessing nested properties in objects, you could also consider using other libraries or frameworks that provide similar functionality, such as: * **Underscore.js**: Another popular utility library that provides a `get()` function. * **Immutable.js**: A library that provides immutable data structures and functions for working with them. * **ES6 Object Accessors**: You could also compare performance using only ES6 object accessors, without any libraries. In terms of native JavaScript alternatives, you could consider using recursive functions or iterative approaches to access nested properties in objects. For example: ```javascript function get(obj, path, defaultValue) { const keys = path.split('.'); let value = obj; for (let i = 0; i < keys.length; i++) { if (!value[keys[i]]) break; value = value[keys[i]]; } return value === undefined ? defaultValue : value; } ``` Keep in mind that these alternatives may require more boilerplate code and may not be as readable or maintainable as using a library like Lodash.
Related benchmarks:
optional chaining vs lodash get
Lodash toString vs js String Constructor
Native Object.values().some() vs lodash _.some()
Lodash has vs Native Javascript
Comments
Confirm delete:
Do you really want to delete benchmark?