@jrc03c/js-math-tools
Version:
some math tools for JS
26 lines (22 loc) • 552 B
JavaScript
import { assert } from "./assert.mjs"
import { isFunction } from "./is-function.mjs"
function time(fn) {
assert(isFunction(fn), "`fn` must be a function!")
const start = performance.now()
const temp = fn()
if (temp instanceof Promise) {
return new Promise((resolve, reject) => {
try {
temp.then(() => {
const elapsed = performance.now() - start
resolve(elapsed)
})
} catch (e) {
return reject(e)
}
})
} else {
return performance.now() - start
}
}
export { time }