MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / median

Function median

test/compute-engine/benchmarks/parse.benchmark.test.ts:34–40  ·  view source on GitHub ↗
(xs: number[])

Source from the content-addressed store, hash-verified

32const MEASURED_RUNS = 5;
33
34function median(xs: number[]): number {
35 const sorted = [...xs].sort((a, b) => a - b);
36 const mid = Math.floor(sorted.length / 2);
37 return sorted.length % 2 === 0
38 ? (sorted[mid - 1] + sorted[mid]) / 2
39 : sorted[mid];
40}
41
42/** Median time of `MEASURED_RUNS` runs of `fn()`, in ms. */
43function bench(fn: () => void, warmup = 3): number {

Callers 1

benchFunction · 0.70

Calls 1

floorMethod · 0.45

Tested by

no test coverage detected