MeasureThat.net icon indicating copy to clipboard operation
MeasureThat.net copied to clipboard

Async code is not well supported: test not wait for it to finished

Open kLiHz opened this issue 1 year ago • 0 comments

For example I have a wait funtion that returns a Promise which resolves after the timer runs out.

function wait(ms) {
  return new Promise(res => setTimeout(() => { res(ms); }, ms));
}

If I tried using await wait(100); in test cases, it said that top-level await is not supported.

And it doesn't seem to wait until the promise is resolved when I just call wait() and don't await on it, since I'll get a similar benchmark between wait(50) and wait(100). The benchmark is also very high, like several tens of thousands ops per second. If we consider an async code finished after its promise is returned, the benchmark should be less than 20 ops/sec for wait(50), and less than 10 ops/sec for wait(100).

It looks like jsbench.me supports measuring async code after it's finished if user checked the "defer" checkbox. It will mark the test done when deferrered.resolve() is called. We could chain a function to a promise and the test would wait wait on it.

wait(50).then(() => { deferred.resolve(); })

And jsbenchmark.com supports the use of await. I made several tests about wait on it;

kLiHz avatar Apr 28 '24 17:04 kLiHz