Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
The performance cost of try catch
(version: 2)
Comparing performance of:
plain vs try catch
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = 0 function f () { a += 0.01 if (a < 0) { throw new Error() } }
Tests:
plain
f()
try catch
try { f() } catch (e) { console.log(e) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
plain
try catch
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:142.0) Gecko/20100101 Firefox/142.0
Browser/OS:
Firefox 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
plain
443340640.0 Ops/sec
try catch
441423776.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested on MeasureThat.net. The benchmark definition JSON represents two test cases: **Script Preparation Code** ```javascript var a = 0; function f() { a += 0.01; if (a < 0) { throw new Error(); } } ``` This code defines a function `f()` that increments a variable `a` by 0.01 every time it's called. If `a` ever becomes negative, an error is thrown. **Options being compared** The benchmark compares two approaches: 1. **Plain execution**: The test simply calls the `f()` function without any try-catch block. 2. **Try-catch execution**: The test wraps the call to `f()` in a try-catch block, which catches and logs any errors thrown by the function. **Pros and Cons** * **Plain execution**: + Pros: Simpler, less overhead due to error handling. + Cons: May not accurately represent real-world scenarios where exceptions are thrown. * **Try-catch execution**: + Pros: More realistic, as try-catch blocks are commonly used in JavaScript to handle errors. + Cons: Adds overhead due to the extra logic for catching and logging errors. **Library usage** There is no explicit library mentioned in the benchmark definition. However, it's likely that MeasureThat.net uses a hidden library or framework to execute the benchmarks and provide the results. **Special JS features/syntax** None are explicitly mentioned. The code uses standard JavaScript syntax and features. Now, let's look at the individual test cases: * **plain**: This test case calls the `f()` function without any try-catch block. * **try catch**: This test case wraps the call to `f()` in a try-catch block, which catches and logs any errors thrown by the function. **Other alternatives** Some alternative approaches could be explored, such as: 1. **No-op execution**: Instead of calling the `f()` function, simply execute some no-op code (e.g., a constant value) to test the function's overhead. 2. **Loop-based execution**: Call the `f()` function in a loop to simulate repeated executions and measure the overhead. 3. **Async execution**: Use async/await or Promises to test the function's behavior under asynchronous conditions. These alternative approaches might provide additional insights into the performance characteristics of the `f()` function, but they would require modifications to the benchmark definition and execution code. That's a summary of what's being tested on MeasureThat.net!
Related benchmarks:
toFixed vs toPrecision vs Math.round() vs Math.floorfast vs MDN round_to_precision
lodash.round VS Math.round with precision
toFixed vs toPrecision vs Math.round() with constant multiplier
Comparing performance of Math.floor(x); vs Math.round(x); vs (0.5 + x) << 0;
toFixed vs Math.round vs |(bitwise or)
Comments
Confirm delete:
Do you really want to delete benchmark?