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;
}
(async function(){
await delayA(0);
test();
return;
})();
(async function(){
await delayT(test,0);
return;
})();
(async function(){
setTimeout(test,0);
})();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
delayA | |
delayT | |
setTimeout |
Test name | Executions per second |
---|---|
delayA | 293965.7 Ops/sec |
delayT | 225715.0 Ops/sec |
setTimeout | 711639.9 Ops/sec |
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:
test
) in a new function that is executed after the specified wait time, using a promise internally.test
function.Pros and Cons
await delayA
, and may lead to unnecessary overhead due to the wrapper function.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:
setTimeout
.Overall, this benchmark provides a clear and concise way to compare the performance of different approaches to delay function execution in JavaScript.