Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
await delay vs setTimeout
(version: 0)
Comparing performance of:
delayA vs delayT vs setTimeout
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const delayA = time => new Promise(res=>setTimeout(res,time)); const delayT = (func, wait) => { var args = slice.call(arguments, 2); return setTimeout(function(){ return func.apply(null, args); }, wait); }; var test = function(){ return; }
Tests:
delayA
(async function(){ await delayA(0); test(); })();
delayT
(async function(){ delayT(test,0); })();
setTimeout
(async function(){ setTimeout(test,0); })();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
delayA
delayT
setTimeout
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'd be happy to explain the JavaScript benchmark on MeasureThat.net. **Overview** The benchmark tests three approaches for introducing delays in asynchronous code: `await delayA`, `delayT` (a wrapper around `setTimeout`), and `setTimeout` directly. **Options Compared** 1. **`await delayA`**: This approach uses a custom promise-based delay function, `delayA`. It returns a promise that resolves after the specified time. 2. **`delayT`**: This is a modified version of `setTimeout`, which wraps its arguments in an array and applies them to a new function with the original timeout value. The main difference is that it uses `apply()` instead of passing arguments directly, as shown in the benchmark code. 3. **`setTimeout`**: A direct call to the native `setTimeout` function. **Pros and Cons** * **`await delayA`**: + Pros: This approach is idiomatic for JavaScript promises, making it easy to read and understand. It also avoids the need to use `setTimeout` directly. + Cons: The custom promise-based delay function may incur overhead compared to native `setTimeout`. * **`delayT`**: + Pros: This implementation preserves the original arguments passed to `setTimeout`, which can be useful in certain scenarios. It's also a simple and straightforward approach. + Cons: Using `apply()` might incur additional overhead, making it slightly slower than `await delayA` or native `setTimeout`. * **`setTimeout`**: + Pros: This is the most efficient option, as it leverages the native JavaScript engine for scheduling tasks. It's also the most straightforward approach. + Cons: This approach can be less readable due to its direct call to a native function. **Library and Special JS Features** The test uses no external libraries or special JavaScript features (such as Web Workers or async/await polyfills). The custom `delayA` function is used only within the benchmark code, not in any production environment. **Other Alternatives** Some alternative approaches for introducing delays could include: * Using `Promise.then()` instead of promises or native `setTimeout`. * Implementing a timer using Node.js's `setImmediate()` function. * Utilizing Web Workers to schedule tasks asynchronously.
Related benchmarks:
Array.prototype.slice vs spread operator 123
Array.slice vs Array.concat vs Spread operator
Array.slice vs Array.concat vs Spread operator v2
Array.prototype.slice vs spread op
Spread Operator VS Array.prototype.slice() VS Array.prototype.slice(0)
Comments
Confirm delete:
Do you really want to delete benchmark?