Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Way of retrieving nullable value
(version: 1)
Comparing performance of:
Lodash when not null vs Try... Catch w/o Exception vs Lodash when undefined vs Try... Catch w/ Exception vs Native
Created:
7 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
Script Preparation code:
var person = {name: {first: 'Johnson', last: 'Wang' }, lastName: 'Corcino Alejo'}; var stringValue = (expression, defaultValue) => { try { return expression(); } catch (ex) { return defaultValue || ''; } }
Tests:
Lodash when not null
_.get(person, 'name.last');
Try... Catch w/o Exception
stringValue( () => person.name.last );
Lodash when undefined
_.get(person, 'name.last.age');
Try... Catch w/ Exception
stringValue( () => person.name.last.age );
Native
person.name.last
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Lodash when not null
Try... Catch w/o Exception
Lodash when undefined
Try... Catch w/ Exception
Native
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):
I'll break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures how fast JavaScript can retrieve nullable values using different approaches: native code, Lodash library with `_.get()`, and a custom implementation called `stringValue()`. **Options Compared** There are four options compared: 1. **Native**: Directly accessing the property `person.name.last` without any indirection or error handling. 2. **Try... Catch w/o Exception**: Using a try-catch block to handle an exception when attempting to access `person.name.last.age`. 3. **Try... Catch w/ Exception**: Similar to the previous option, but with an actual exception thrown when attempting to access `person.name.last.age`. 4. **Lodash when not null** and **Lodash when undefined**: Using Lodash's `_.get()` function to retrieve nested properties from the `person` object. **Pros and Cons** Here are some pros and cons of each approach: * **Native**: Fastest, as it directly accesses the property without any overhead. However, it may throw an error if the property doesn't exist. * **Try... Catch w/o Exception**: Handles the case where the property doesn't exist by throwing an exception, which can be caught and handled elsewhere in the code. This approach is more robust but may incur a slight performance penalty due to the try-catch block. * **Try... Catch w/ Exception**: Throws an actual exception when attempting to access `person.name.last.age`, which can be caught and handled by the caller. This approach provides more explicit error handling but incurs a performance penalty due to the exception throwing. * **Lodash when not null** and **Lodash when undefined**: Lodash's `_.get()` function is a convenient way to retrieve nested properties without worrying about null values. However, it may incur a performance penalty compared to direct access or try-catch approaches. **Library: _.get()** Lodash's `_.get()` function is a utility function that retrieves a nested property from an object. It takes two arguments: the first value (the object) and the key(s) of the property to retrieve. If any of the keys are null or undefined, it returns the first non-null/undefined value. **Special JS Feature/Syntax** There isn't any special JavaScript feature or syntax used in this benchmark, other than using the `try`-`catch` block for error handling. **Alternatives** Some alternative approaches could be: * Using a more robust error handling mechanism, such as throwing a custom exception class instead of relying on try-catch blocks. * Using a different library, such as Moment.js, to handle date-related operations (none are present in this benchmark). * Optimizing the performance of Lodash's `_.get()` function by implementing it in native code or using a Just-In-Time (JIT) compiler. Overall, this benchmark provides a useful comparison of different approaches for retrieving nullable values in JavaScript, helping developers understand the trade-offs between direct access, try-catch blocks, and library functions.
Related benchmarks:
Lodash vs native
Lodash.get vs Property dot notation with sanity check
Lodash.get vs Property dot notation for defaults
Lodash.get vs Property bracket notation
Lodash.get vs Property dot notation @movlan
Comments
Confirm delete:
Do you really want to delete benchmark?