Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Improved Lodash get vs my get
(version: 0)
Comparing performance of:
My Get vs Lodash Get
Created:
4 years ago
by:
Guest
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']; var isObject = input => typeof input === 'object' || typeof input === 'function'; var myGet = (input, path, defaultValue) => { if (!input || isObject(input) || !path.length) { return defaultValue; } let result = input; for (let i = 0; i < path.length; i += 1) { if (isObject(result) && Reflect.has(result, path[i])) { result = result[path[i]]; } else { return defaultValue; } } return result; };
Tests:
My Get
myGet(testData, testPath);
Lodash Get
_.get(testData, testPath);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
My Get
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 break down the provided JSON and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Definition** The benchmark definition is a JSON object that describes the test case. It includes: * `Name`: The name of the benchmark ("Improved Lodash get vs my get"). * `Description`: An optional description of the benchmark (null in this case). * `Script Preparation Code`: A JavaScript code snippet that sets up the test data and variables. * `Html Preparation Code`: A link to a CDN-hosted library (Lodash) that will be used in the benchmark. The script preparation code defines: * `testData`: An object with nested properties (`a`, `b`, `c`, etc.) representing the data structure being tested. * `testPath`: An array of keys representing the path to access a specific property within the `testData` object. * `isObject`: A function that checks if an input is an object or a function. * `myGet`: A custom function (`myGet`) that takes an input, path, and default value as arguments. It uses a loop to traverse the object using the provided path and returns the result or a default value if not found. **Individual Test Cases** There are two individual test cases: 1. "My Get" * `Benchmark Definition`: Calls the `myGet` function with `testData` and `testPath`. 2. "Lodash Get" * `Benchmark Definition`: Calls the `_.get` function from Lodash with `testData` and `testPath`. **Comparison** The two test cases compare the performance of the custom `myGet` function versus the Lodash `_get` function when accessing nested properties in an object. **Pros and Cons of each approach:** * **Custom `myGet` function:** + Pros: - Can be customized to handle specific use cases or edge cases. - May be more efficient for simple, flat objects. + Cons: - Requires manual iteration over the object properties, which can lead to slower performance for deep or complex data structures. - Less readable and maintainable compared to using a dedicated library like Lodash. * **Lodash `_get` function:** + Pros: - Highly optimized and tested for performance. - Provides a robust set of features for accessing nested properties, including support for arrays and objects. - Well-documented and widely used in the JavaScript community. + Cons: - Requires importing an additional library (Lodash). - May have slower performance compared to a custom implementation for very simple or flat data structures. **Other considerations:** * The use of `Reflect.has` in the `myGet` function is a modern JavaScript feature that allows dynamic property access. If this feature is not supported by older browsers, it may affect compatibility. * The benchmark result includes the device platform and operating system, which can impact performance. However, these factors are not directly related to the comparison between `myGet` and Lodash `_get`. **Alternatives:** For a custom implementation of nested property access, you could consider using: * `Array.prototype.reduce()` or `Array.prototype.forEach()` * A recursive function that traverses the object properties Alternatively, if you prefer not to implement a custom solution, you can use other libraries like: * `get-object-by-path` (a lightweight library specifically designed for accessing nested properties) * `prop-access` (another library with similar functionality to Lodash `_get`)
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?