Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
try vs try callback
(version: 0)
Comparing performance of:
try loop vs try callback loop
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function doSomething() { let k = 0; for (let i = 0; i < 100000; i++) { k += i; } }
Tests:
try loop
for(let i=0;i<1000;i++){ try{ doSomething(); } catch(e) { console.log(e); } }
try callback loop
function tryCb(callback, ref) { try { callback(); } catch (e) { log.error(`unhandled exception at ${'' + callback} ${ref ? `(ref: ${ref})` : ""}: \n` + e.name + '\n' + e.stack); } } for(let i=0;i<1000;i++){ tryCb(()=>doSomething()); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
try loop
try callback loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
try loop
16.7 Ops/sec
try callback loop
16.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark. **Benchmark Definition** The benchmark is defined by two scripts: `doSomething()` and an empty function (which we'll assume is used as a callback). The purpose of these scripts is to simulate some work that needs to be done. * `doSomething()` increments a variable `k` from 0 to 100,000, which suggests it's a simple loop. * The empty function is likely used as a placeholder or a callback function in the benchmark. **Test Cases** There are two test cases: 1. **try loop**: This test case uses a traditional `for` loop with `try-catch` blocks to execute `doSomething()` inside each iteration. If an exception occurs, it's caught and logged to the console. 2. **try callback loop**: This test case uses a function `tryCb()` that takes a callback function as an argument. The callback function is then executed inside the loop using this `tryCb()` function. If an exception occurs in the callback function, it's caught by `tryCb()` and logged to the console. **Options Compared** The benchmark compares two approaches: * **Traditional try-catch**: This approach uses a separate `try` block for each iteration of the loop. * **Try-callback with wrapper function**: This approach uses a single `try` block that wraps the entire loop, and passes the callback function as an argument to this wrapper function. **Pros and Cons** 1. **Traditional try-catch**: * Pros: Easy to understand and implement, can handle exceptions in a straightforward way. * Cons: May introduce additional overhead due to the extra `try` block per iteration. 2. **Try-callback with wrapper function**: * Pros: Can potentially reduce overhead by using a single `try` block for the entire loop, and reusing the same exception handling logic. * Cons: May be harder to understand and implement, as it introduces an additional abstraction layer. **Library** In this benchmark, there is no explicit library mentioned. However, MeasureThat.net's framework likely includes some underlying libraries or frameworks that enable the creation of these test cases and the execution of benchmarks. **Special JS Features/Syntax** There are a few special features in this benchmark: * **Try-catch**: This is a built-in JavaScript feature for handling exceptions. * **Callback functions**: Callback functions are a common pattern in JavaScript, often used to pass function references as arguments to other functions. Other alternatives that could be considered include: * Using a more modern approach like async/await or Promises for exception handling. * Using a different concurrency model, such as using threads or worker processes. * Optimizing the loop itself, perhaps by using parallel processing or vectorization.
Related benchmarks:
letvvar
increment compare
null vs undefined iiffe
let vs var for loop
Million loops
Comments
Confirm delete:
Do you really want to delete benchmark?