Skip to content

Instantly share code, notes, and snippets.

@DanielRamosAcosta
Last active November 5, 2021 19:38
Show Gist options
  • Save DanielRamosAcosta/e36646e5d0c38df07fdf224644eac668 to your computer and use it in GitHub Desktop.
Save DanielRamosAcosta/e36646e5d0c38df07fdf224644eac668 to your computer and use it in GitHub Desktop.
import { BenchmarkRunResult } from "https://deno.land/std/testing/bench.ts";
function typicalDeviation(data: number[]) {
const avg = data.reduce((a, b) => a + b) / data.length;
const squareDiffs = data.map((value) => {
const diff = value - avg;
const sqrDiff = diff * diff;
return sqrDiff;
});
const avgSquareDiff = squareDiffs.reduce((a, b) => a + b) / data.length;
const stdDev = Math.sqrt(avgSquareDiff);
return stdDev;
}
export function denoBenchToBenchmarkJsOutput(result: BenchmarkRunResult) {
return result.results.map((r) => ({
opsPerSec: Math.round(1000 / r.measuredRunsAvgMs),
error: Math.round(typicalDeviation(r.measuredRunsMs) * 100) / 100,
name: r.name,
runs: r.runsCount,
})).map((r) =>
`${r.name} x ${r.opsPerSec} ops/sec ±${r.error}% (${r.runs} runs sampled)`
).join("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment