(values)
| 36 | const benchmarkStartedAt = Date.now() |
| 37 | |
| 38 | function aggregateNumbers(values) { |
| 39 | const sorted = [...values].sort((left, right) => left - right) |
| 40 | if (sorted.length === 0) { |
| 41 | return { |
| 42 | median: null, |
| 43 | p95: null, |
| 44 | min: null, |
| 45 | max: null, |
| 46 | mean: null, |
| 47 | standardDeviation: null, |
| 48 | spreadPercent: null, |
| 49 | trimmedCount: 0, |
| 50 | originalCount: 0, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // IQR-based outlier trimming (only when enough samples) |
| 55 | let trimmed = sorted |
| 56 | if (sorted.length >= 8) { |
| 57 | const q1Index = Math.floor(sorted.length * 0.25) |
| 58 | const q3Index = Math.floor(sorted.length * 0.75) |
| 59 | const q1 = sorted[q1Index] |
| 60 | const q3 = sorted[q3Index] |
| 61 | const iqr = q3 - q1 |
| 62 | const lowerFence = q1 - 1.5 * iqr |
| 63 | const upperFence = q3 + 1.5 * iqr |
| 64 | trimmed = sorted.filter((value) => value >= lowerFence && value <= upperFence) |
| 65 | // Fall back to original if trimming removed too many samples |
| 66 | if (trimmed.length < sorted.length * 0.5) { |
| 67 | trimmed = sorted |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | const middle = Math.floor(trimmed.length / 2) |
| 72 | const median = |
| 73 | trimmed.length % 2 === 0 ? (trimmed[middle - 1] + trimmed[middle]) / 2 : trimmed[middle] |
| 74 | const mean = trimmed.reduce((total, value) => total + value, 0) / trimmed.length |
| 75 | const variance = trimmed.reduce((total, value) => total + (value - mean) ** 2, 0) / trimmed.length |
| 76 | const standardDeviation = Math.sqrt(variance) |
| 77 | const p95 = trimmed[Math.min(trimmed.length - 1, Math.ceil(trimmed.length * 0.95) - 1)] |
| 78 | |
| 79 | return { |
| 80 | median, |
| 81 | p95, |
| 82 | min: trimmed[0], |
| 83 | max: trimmed[trimmed.length - 1], |
| 84 | mean, |
| 85 | standardDeviation, |
| 86 | spreadPercent: median === 0 ? null : ((p95 - median) / Math.abs(median)) * 100, |
| 87 | trimmedCount: trimmed.length, |
| 88 | originalCount: sorted.length, |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | function formatMs(value) { |
| 93 | return typeof value === 'number' ? `${value.toFixed(2)} ms` : 'timeout' |
no outgoing calls
no test coverage detected