Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
safeGet comparison (for PR)
(version: 0)
Comparing performance of:
old safeGet vs dlv (array notation) vs safeGet not using variables as params vs safeGet not 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']);
safeGet not 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
safeGet not 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):
**Benchmark Explanation** The provided benchmark tests the performance of three variants of the `safeGet` function, which is designed to safely access nested properties in an object. **Options Compared** The benchmark compares the following options: 1. **Old safeGet**: The original implementation of `safeGet`, which uses a loop to iterate over the paths and checks if each property exists before accessing it. 2. **DLV (array notation)**: A new implementation of `safeGet` that uses array notation to access nested properties, making it more concise but potentially slower. 3. **SafeGet without variables as params**: Two variants of the original `safeGet` implementation that pass the paths and default value as separate arguments, rather than using variables. 4. **SafeGet with variables as params**: The latest implementation of `safeGet`, which uses variables to store the paths and default value, making it more concise and potentially faster. **Pros and Cons** * **Old safeGet**: + Pros: Easy to understand and maintain. + Cons: May be slower due to the loop-based approach. * **DLV (array notation)**: + Pros: More concise and potentially faster. + Cons: May be less readable and harder to maintain. * **SafeGet without variables as params**: + Pros: Can be more intuitive for some developers, especially those familiar with functional programming concepts. + Cons: May lead to more boilerplate code and harder-to-read implementation. * **SafeGet with variables as params**: + Pros: More concise, potentially faster, and easier to read than the previous implementations. + Cons: May require additional error handling for variable scope. **Library** None of the provided benchmark tests use a library. The `safeGet` function is implemented directly in JavaScript. **Special JS Features or Syntax** The implementation uses some modern JavaScript features, such as: * Arrow functions (`=>`) * Destructuring assignments (`const [x, y] = arr;`) * Object property access using bracket notation (`obj[prop]`) However, these features are not specific to the benchmark and can be used in general-purpose JavaScript development. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: 1. **Using a recursive approach**: Instead of using loops or array notation, you could implement `safeGet` recursively, which might lead to more concise code but also potential performance issues. 2. **Using a library like Lodash**: If you prefer a more functional programming style, you could use a library like Lodash, which provides utility functions for working with arrays and objects. In general, the choice of implementation depends on your personal preference, the specific requirements of your project, and the trade-offs between conciseness, readability, and performance.
Related benchmarks:
babel transpiled optional chaining vs safeGet
babel transpiled optional chaining vs safeGet vs optional chaining
safeGet comparison (PR)
safeGet comparison (for improvement PR)
Comments
Confirm delete:
Do you really want to delete benchmark?