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(); return; })();
delayT
(async function(){ await delayT(test,0); return; })();
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0
Browser/OS:
Firefox 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
delayA
293965.7 Ops/sec
delayT
225715.0 Ops/sec
setTimeout
711639.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its components. **Benchmark Definition** The benchmark is comparing three approaches: `await delayA`, `await delayT`, and `setTimeout`. The script preparation code defines two functions: * `delayA`: returns a promise that resolves after a specified time, using `setTimeout` internally. * `delayT`: wraps the provided function (`test`) in a new function, which is then executed after a specified wait time, also using `setTimeout`. **Options being compared** The three options are: 1. **await delayA**: uses a promise to delay execution, with the promise resolving after the specified time. 2. **await delayT**: wraps the provided function (`test`) in a new function that is executed after the specified wait time, using a promise internally. 3. **setTimeout**: directly sets a timeout for the execution of the `test` function. **Pros and Cons** * **await delayA**: + Pros: simple and readable implementation, uses promises to handle asynchronous operations. + Cons: may lead to unnecessary context switching or object allocation during the promise resolution phase. * **await delayT**: + Pros: provides a more explicit way to wrap functions in promises, can be useful for chaining delays or other async operations. + Cons: adds extra complexity compared to `await delayA`, and may lead to unnecessary overhead due to the wrapper function. * **setTimeout**: + Pros: widely supported and well-established method for setting timers, often used in production code. + Cons: can be less readable and maintainable than other approaches, especially for complex timing requirements. **Library Usage** There is no explicit library usage mentioned in the benchmark definition or test cases. However, it's worth noting that `slice.call` is used to create an array of arguments from the `arguments` object, which is a common pattern in JavaScript. **Special JS Features or Syntax** There are no special features or syntax used in this benchmark beyond what's standard for JavaScript (async/await, promises). However, it's worth noting that the use of `var args = slice.call(arguments, 2);` might be considered outdated or less readable than using `...args` or other spread operator methods. **Alternatives** Other alternatives to these approaches could include: * **useCallback**: a React hook for memoizing functions, which can also be used to delay function execution. * **setInterval**: another widely supported method for setting timers, although its usage might be less explicit than `setTimeout`. * **Promise.race**: a built-in promise constructor that resolves as soon as any of the promises in an array resolve, which could be used to implement similar timing behavior. Overall, this benchmark provides a clear and concise way to compare the performance of different approaches to delay function execution in JavaScript.
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?