export function quantile(values, proportion) { const sorted = [...values].sort((a, b) => a - b); return sorted.length ? sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * proportion))] : 0; } export const round = (value) => Math.round(value * 100) / 100; export function frameMetrics(useful, raf) { const sum = (index) => round( useful.reduce((total, frame) => total + frame[index], 0) / 1000, ); const max = (index) => round( Math.max(0, ...useful.map((frame) => frame[index])) / 1000, ); const p95 = (index) => round(quantile( useful.map((frame) => frame[index] / 1000), .95, )); return { frames: useful.length, buildSumMs: sum(0), buildMaxMs: max(0), buildP50Ms: round(quantile(useful.map(f => f[0] / 1000), .5)), buildP95Ms: p95(0), rasterSumMs: sum(1), rasterMaxMs: max(1), rasterP95Ms: p95(1), totalP50Ms: round(quantile(useful.map(f => f[2] / 1000), .5)), totalP95Ms: p95(2), totalMaxMs: max(2), rafP50Ms: round(quantile(raf.deltas, .5)), rafOver33: raf.deltas.filter(x => x > 33.34).length, over16: useful.filter((frame) => frame[2] > 16667).length, rafP95Ms: round(quantile(raf.deltas, .95)), rafMaxMs: round(Math.max(0, ...raf.deltas)), longTasks: raf.longTasks.length, longTaskMs: round(raf.longTasks.reduce((total, duration) => total + duration, 0)), }; }