asynchronously time the execution of an async function. if you are going to provide a synchronous function with certainty, then you might be better off using timeIt instead.
import { assertGreater, assertLess } from "jsr:@std/assert"const asyncTestFn = async (a: number, b: number): Promise<number> => new Promise((resolve) => { setTimeout(() => { resolve(a + b) }, 300)})const delta_time = await asyncTimeIt(asyncTestFn, 5, 10), log = `execution time: ${delta_time} ms`assertGreater(delta_time, 290) // completing the promise should take more than `290` millisecondsassertLess(delta_time, 400) // completing the promise should take less than `400` milliseconds Copy
import { assertGreater, assertLess } from "jsr:@std/assert"const asyncTestFn = async (a: number, b: number): Promise<number> => new Promise((resolve) => { setTimeout(() => { resolve(a + b) }, 300)})const delta_time = await asyncTimeIt(asyncTestFn, 5, 10), log = `execution time: ${delta_time} ms`assertGreater(delta_time, 290) // completing the promise should take more than `290` millisecondsassertLess(delta_time, 400) // completing the promise should take less than `400` milliseconds
Rest
millisecond time for function execution.
asynchronously time the execution of an async function. if you are going to provide a synchronous function with certainty, then you might be better off using timeIt instead.
Example