Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
testing the performance of try catch vs check for undefined
(version: 0)
Comparing performance of:
using try catch block vs using check
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
using try catch block
const obj = { obj : { obj: { obj: { obj: 1 } } } } for (let i=0; i<99; i++) { try { const val = obj.obj.obj.obj.obj; } catch (e) { //do nothing } }
using check
const obj = { obj : { obj: { obj: { obj: 1 } } } } const getNested = obj => (path) => { const arr = path.split('.'); return arr.reduce( (root, key) => ((root && root[key] !== 'undefined') ? root[key] : undefined), obj, ); }; for (let i=0; i<99; i++) { getNested(obj)('obj.obj.obj.obj'); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
using try catch block
using check
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0
Browser/OS:
Firefox 125 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
using try catch block
10146887.0 Ops/sec
using check
59776.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare two approaches: using `try-catch` blocks and checking for `undefined` manually. **Approach 1: Using try-catch block** In this approach, a `try` block is used to access a deeply nested object within an object. If the property does not exist at that level, a `catch` block catches the error and allows the execution to continue. ```javascript try { const val = obj.obj.obj.obj.obj; } catch (e) {} ``` **Approach 2: Checking for undefined manually** In this approach, a custom function `getNested` is used to access the same deeply nested object. The function uses a recursive approach with array reduction to iterate through a path string and check if each property exists at that level. ```javascript const getNested = obj => (path) => { const arr = path.split('.'); return arr.reduce( (root, key) => ((root && root[key] !== 'undefined') ? root[key] : undefined), obj, ); }; getNested(obj)('obj.obj.obj.obj'); ``` **Pros and Cons of each approach** * **Try-catch block:** + Pros: - More concise and readable code - Less chance of incorrect results due to manual property checks + Cons: - May introduce unnecessary overhead due to error handling - Can lead to unexpected behavior if not handled correctly * **Checking for undefined manually:** + Pros: - More explicit control over the access process - Can be more efficient in terms of execution speed + Cons: - More verbose and potentially error-prone code - Requires manual property checks to avoid errors **Library** There is no specific library mentioned in the benchmark definition. However, the `getNested` function uses a simple recursive approach with array reduction. **Special JavaScript feature or syntax** There are no special JavaScript features or syntaxes used in this benchmark. The code relies on standard language constructs such as objects, arrays, and loops. **Other alternatives** Other approaches to access deeply nested objects could include: * Using a library like Lodash's `get` method * Utilizing a JavaScript framework like React's `useCallback` hook * Implementing a custom recursive function with explicit type checking Keep in mind that the choice of approach depends on the specific use case and performance requirements. **Benchmark preparation code** The benchmark preparation code is empty, which means that no additional setup or configuration is required to run the test.
Related benchmarks:
Try/Catch vs Typeof
Testing for false vs === undefined for undefined member
Testing for false vs === undefined vs hasOwnProperty for undefined member
Testing for false vs === undefined vs hasOwnProperty for undefined member
Testing for false vs === undefined vs hasOwnProperty vs in for undefined member
Comments
Confirm delete:
Do you really want to delete benchmark?