Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
safeGet comparison (for improvement PR)
(version: 0)
Comparing performance of:
old safeGet vs dlv (array notation) vs safeGet NOT using variables as params vs safeGe using variables as params (this PR code)
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var state = { wrapped1: { wrapped2: { location: { search: '', id: 12 } } }, data: {} }; var safeGet = function(nestedObj, paths, defaultVal) { if (!paths || paths.length === 0) return defaultVal const result = paths.slice(0).reduce((obj, path, i, arr) => { if (!obj) { arr.splice(i) // break out of the loop return undefined } return obj[path] }, nestedObj) if (result === undefined || result === null) { return defaultVal } return result } var safeGet2 = function(nestedObj, paths, defaultVal) { if (!paths || paths.length === 0) return defaultVal for (let p = 0; p < paths.length; p++) { nestedObj = nestedObj ? nestedObj[paths[p]] : undefined } return nestedObj === undefined || nestedObj === null ? defaultVal : nestedObj } var safeGet3 = function(nestedObj, paths, defaultVal, p, undef) { if (!paths || paths.length === 0) return defaultVal for (p = 0; p < paths.length; p++) { nestedObj = nestedObj ? nestedObj[paths[p]] : undef } return nestedObj === undef || nestedObj === null ? defaultVal : nestedObj } var dlv = function(obj, key, def, p, undef) { key = key.split ? key.split('.') : key; for (p = 0; p < key.length; p++) { obj = obj ? obj[key[p]] : undef; } return obj === undef ? def : obj; }
Tests:
old safeGet
var l = safeGet(state, ['wrapped1','wrapped2','location','search']);
dlv (array notation)
dlv(state, ['wrapped1','wrapped2','location','search']);
safeGet NOT using variables as params
safeGet2(state, ['wrapped1','wrapped2','location','search']);
safeGe using variables as params (this PR code)
safeGet3(state, ['wrapped1','wrapped2','location','search']);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
old safeGet
dlv (array notation)
safeGet NOT using variables as params
safeGe using variables as params (this PR code)
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 provided benchmark JSON. **Benchmark Overview** The benchmark compares three different implementations of a `safeGet` function, which appears to be designed to safely navigate nested object hierarchies while providing default values for missing keys. **Options Compared** 1. **Old Safe Get (`old safeGet`)**: The original implementation using a loop with an array of paths. * Pros: Simple and straightforward. * Cons: May have performance issues due to the loop and potential null checks. 2. **Array Notation (`dlv`)**: An alternative implementation using array notation, which splits the key into individual parts and iterates through them. * Pros: More concise and potentially more readable. * Cons: May require additional parsing and logic for nested keys. 3. **New Safe Get (`safeGet2`)**: A modified implementation without variables as parameters. * Pros: Simplifies the code by removing variable usage. * Cons: May be less readable due to reduced expressiveness. 4. **New Safe Get with Variables (`safeGet3`)**: The latest implementation using variables as parameters, which is the focus of the current improvement PR. * Pros: Provides more flexibility and readability through the use of variables. * Cons: May introduce additional complexity or performance overhead. **Library Used** None explicitly mentioned in the provided JSON. However, it's likely that each implementation relies on JavaScript's built-in object methods (e.g., `in`, `hasOwnProperty`, etc.) for navigation and null checking. **Special JS Features/Syntax** There is no explicit mention of any special JavaScript features or syntax being used in this benchmark. **Benchmark Preparation Code** The preparation code sets up an object `state` with nested properties, which serves as the input data structure for the different implementations of `safeGet`. **Alternatives** Other alternatives for implementing a similar function could include: 1. Using recursion instead of loops. 2. Utilizing libraries like Lodash or Underscore.js, which provide built-in functions for object navigation and manipulation. 3. Employing more advanced techniques, such as using `Object.getPrototypeOf()` or `Object.create()`, to navigate the object hierarchy. Keep in mind that each alternative approach may introduce trade-offs in terms of performance, readability, and maintainability.
Related benchmarks:
babel transpiled optional chaining vs safeGet
babel transpiled optional chaining vs safeGet vs optional chaining
safeGet comparison (PR)
safeGet comparison (for PR)
Comments
Confirm delete:
Do you really want to delete benchmark?