mirror of
https://github.com/denoland/deno.git
synced 2024-11-29 16:30:56 -05:00
b3ceafaa5d
Enable deno devs to bench/profile/test JS code changes without doing a full --release rebuild. Incremental release builds take ~4mn on M1s, often more on other machines ...
25 lines
675 B
JavaScript
25 lines
675 B
JavaScript
export function benchSync(name, n, innerLoop) {
|
|
const t1 = Date.now();
|
|
for (let i = 0; i < n; i++) {
|
|
innerLoop(i);
|
|
}
|
|
const t2 = Date.now();
|
|
console.log(benchStats(name, n, t1, t2));
|
|
}
|
|
|
|
export async function benchAsync(name, n, innerLoop) {
|
|
const t1 = Date.now();
|
|
for (let i = 0; i < n; i++) {
|
|
await innerLoop(i);
|
|
}
|
|
const t2 = Date.now();
|
|
console.log(benchStats(name, n, t1, t2));
|
|
}
|
|
|
|
function benchStats(name, n, t1, t2) {
|
|
const dt = (t2 - t1) / 1e3;
|
|
const r = n / dt;
|
|
const ns = Math.floor(dt / n * 1e9);
|
|
return `${name}:${" ".repeat(20 - name.length)}\t` +
|
|
`n = ${n}, dt = ${dt.toFixed(3)}s, r = ${r.toFixed(0)}/s, t = ${ns}ns/op`;
|
|
}
|