This project is part of the @thi.ng/umbrella monorepo.
Basic, non-precise benchmarking helpers.
This feature was previously part of the @thi.ng/api package.
yarn add @thi.ng/bench
None
import { timed, bench } from "@thi.ng/bench";
// test functions
const fib = (n) => n > 2 ? fib(n - 1) + fib(n - 2) : n > 0 ? 1 : 0;
const fib2 = (n) => {
const res = [0, 1];
for(let i = 2; i <= n; i++) {
res[i] = res[i - 1] + res[i - 2];
}
return res[n];
};
// measure single execution time
timed(() => fib(40));
// 714ms
// 102334155
timed(() => fib2(40));
// 0ms
// 102334155
// measure 1mil iterations (default)
bench(() => fib(10), 1e6);
// 395ms
// 55
bench(() => fib2(10), 1e6);
// 53ms
// 55
- Karsten Schmidt
© 2018 Karsten Schmidt // Apache Software License 2.0