Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash get vs my get
(version: 9)
Comparing performance of:
My Get vs Lodash Get vs Empty myGet vs Empty Lodash Get
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
var testData = { a: { b: { c: { d: [{ e: { f: { g: { h: [{}, { i: {}, }, ], }, }, }, }, ], }, }, }, }; var testPath = ['a', 'b', 'c', 'd', 1, 'e', 'f', 'g', 'h', 1, 'i']; function isObject(input) { if (input === null) return false; return typeof input === 'object' || typeof input === 'function'; } var myGet = (input, path, defaultValue) => { if (!input || !path.length) { return defaultValue; } let result = input; for (let i = 0; i < path.length - 1; i += 1) { const nextResult = result[path[i]]; if (typeof nextResult !== 'undefined') { result = nextResult; } else { return defaultValue; } } result = result[path[path.length - 1]]; return typeof result === 'undefined' ? defaultValue : result; };
Tests:
My Get
myGet(testData, testPath);
Lodash Get
_.get(testData, testPath);
Empty myGet
myGet(null, testPath);
Empty Lodash Get
_.get(null, testPath);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
My Get
Lodash Get
Empty myGet
Empty Lodash Get
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 dive into the world of JavaScript microbenchmarks on MeasureThat.net. The provided JSON represents a benchmark for comparing the performance of two custom functions, `myGet` and `_.get`, which are designed to navigate through nested objects in JavaScript. **Options being compared:** 1. **Custom function `myGet`**: This function takes three arguments: `input`, `path`, and `defaultValue`. It recursively traverses the object graph using the provided `path` array, starting from the root `input` object. If a key is not found in the path, it returns the `defaultValue`. 2. **Lodash's `_get` function**: This is a built-in function from the Lodash library that provides similar functionality to `myGet`. It also traverses an object graph using a path array and returns a value or a default value if the key is not found. **Pros and cons of each approach:** 1. **Custom function `myGet`**: * Pros: + Lightweight and easy to understand. + Can be optimized for specific use cases. * Cons: + Requires manual handling of edge cases, such as null or undefined inputs. + May not be as efficient as a built-in library like Lodash due to the overhead of recursive function calls. 2. **Lodash's `_get` function**: * Pros: + Well-tested and optimized for performance. + Provides additional features, such as support for nested arrays and functions. * Cons: + Adds external dependencies (the Lodash library). + May have a higher memory footprint due to the inclusion of the library. **Library usage:** In this benchmark, the `lodash` library is included via a CDN link in the HTML preparation code. The `_get` function from Lodash is used as-is in the benchmark, without any modifications or optimizations. **Special JS features/syntax:** None mentioned. **Other considerations:** * This benchmark only compares the performance of the two custom functions on empty objects (`null` input). It would be interesting to see how they perform on non-empty objects. * The benchmark does not consider other factors that might affect performance, such as the size of the object graph or the presence of other JavaScript features (e.g., async/await). * MeasureThat.net's framework likely provides additional features and tools for analyzing and optimizing microbenchmarks. **Alternatives:** Other alternatives to Lodash's `_get` function could be: 1. **Built-in `in` operator**: Instead of using a dedicated library, you can use the `in` operator to check if a key exists in an object. 2. **ES6 `?.` optional chaining operator**: This operator provides a concise way to access nested properties without throwing errors when they are missing. 3. **Custom implementation with `for...in` loop**: You can write your own custom function using a `for...in` loop to iterate over the object's properties and check if the key exists. Keep in mind that these alternatives may have different performance characteristics and trade-offs compared to Lodash's `_get` function.
Related benchmarks:
_.get vs nativefeaf
optional chaining vs lodash get
Lodash get
Lodash test suite
Lodash at vs Native
Comments
Confirm delete:
Do you really want to delete benchmark?