Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test Lodash Get speed
(version: 0)
Comparing performance of:
Lodash Get with string vs Native Get with String vs Lodash Get with Array vs Native Get with Array
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js'></script>
Script Preparation code:
var data = Array(1000000).fill({ a: [{ bar: { c: 3 } }] }); var newGet = (obj, path, defValue) => { // If path is not defined or it has false value if (!path) return undefined // Check if path is string or array. Regex : ensure that we do not have '.' and brackets. // Regex explained: https://regexr.com/58j0k const pathArray = Array.isArray(path) ? path : path.match(/([^[.\]])+/g) // Find value const result = pathArray.reduce((prevObj, key) => prevObj && prevObj[key], obj) // If found value is undefined return default value; otherwise return the value return result === undefined ? defValue : result }
Tests:
Lodash Get with string
_.get(data, 'a[0].bar.c')
Native Get with String
newGet(data, 'a[0].bar.c')
Lodash Get with Array
_.get(data, ['a', '0', 'bar', 'c'])
Native Get with Array
newGet(data, ['a', '0', 'bar', 'c'])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash Get with string
Native Get with String
Lodash Get with Array
Native Get with Array
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 the provided JSON and individual test cases to understand what is being tested. **Benchmark Definition** The benchmark definition is a JavaScript function that tests the performance of two approaches: using Lodash's `_.get()` method and implementing a custom `newGet()` function. The `newGet()` function takes an object, a path, and a default value as arguments and returns the value at the specified path. **Options Compared** The two options being compared are: 1. **Lodash Get**: Using Lodash's `_.get()` method to access nested properties of an object. 2. **Native Get**: Implementing a custom `newGet()` function to access nested properties of an object. **Pros and Cons** **Lodash Get:** Pros: * More readable and maintainable code, as the path is defined using a clear and concise syntax. * Less prone to errors, as Lodash takes care of handling missing properties and default values. Cons: * Performance overhead due to the use of a library function. * May not be optimized for performance, depending on the implementation. **Native Get:** Pros: * Potential performance benefits, as it avoids the overhead of a library function. * More control over the implementation, allowing for optimization and customization. Cons: * More complex code, which can lead to errors if not implemented correctly. * Less readable and maintainable, especially for non-experts. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, object creation, and string processing. In this case, the `_.get()` method is used to access nested properties of an object. The `_.get()` function takes three arguments: * An object (`obj`) * A path (a string or an array of strings) * A default value (an optional argument) If the path is not defined or has a false value, the function returns undefined. Otherwise, it iterates over the path using a regular expression to extract each key, and recursively calls itself until it finds the desired value. **Special JS Feature: None** There are no special JavaScript features or syntaxes being used in this benchmark. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: 1. **Underscore.js**: Another popular JavaScript utility library that provides similar functionality to Lodash. 2. **Moment.js**: A library for working with dates and times, which can be useful if you need to access nested properties of date objects. 3. **ES6 Object Accessors**: You could use the new object accessors introduced in ES6, such as `?.` (safe navigation) or optional chaining (`?.[key]`) to access nested properties. Keep in mind that these alternatives may have different performance characteristics or code readability implications compared to Lodash or the custom `newGet()` function.
Related benchmarks:
Test Lodash Get speed V2
Test Lodash Get speed V3
Test Lodash Get speed V3.1
Lodash.get vs Lodash.property vs native test
Comments
Confirm delete:
Do you really want to delete benchmark?