MCPcopy Create free account
hub / github.com/ReactTooltip/react-tooltip / aggregateNumbers

Function aggregateNumbers

benchmarks/aggregate-benchmarks.mjs:16–70  ·  view source on GitHub ↗
(values)

Source from the content-addressed store, hash-verified

14const explicitFiles = args._.map((inputPath) => path.resolve(inputPath))
15
16function aggregateNumbers(values) {
17 const sorted = values
18 .filter((value) => typeof value === 'number' && Number.isFinite(value))
19 .sort((left, right) => left - right)
20
21 if (sorted.length === 0) {
22 return {
23 median: null,
24 p95: null,
25 min: null,
26 max: null,
27 mean: null,
28 standardDeviation: null,
29 spreadPercent: null,
30 sampleCount: 0,
31 trimmedCount: 0,
32 }
33 }
34
35 // IQR-based outlier trimming (only when enough samples)
36 let trimmed = sorted
37 if (sorted.length >= 8) {
38 const q1Index = Math.floor(sorted.length * 0.25)
39 const q3Index = Math.floor(sorted.length * 0.75)
40 const q1 = sorted[q1Index]
41 const q3 = sorted[q3Index]
42 const iqr = q3 - q1
43 const lowerFence = q1 - 1.5 * iqr
44 const upperFence = q3 + 1.5 * iqr
45 trimmed = sorted.filter((value) => value >= lowerFence && value <= upperFence)
46 if (trimmed.length < sorted.length * 0.5) {
47 trimmed = sorted
48 }
49 }
50
51 const middle = Math.floor(trimmed.length / 2)
52 const median =
53 trimmed.length % 2 === 0 ? (trimmed[middle - 1] + trimmed[middle]) / 2 : trimmed[middle]
54 const mean = trimmed.reduce((total, value) => total + value, 0) / trimmed.length
55 const variance = trimmed.reduce((total, value) => total + (value - mean) ** 2, 0) / trimmed.length
56 const standardDeviation = Math.sqrt(variance)
57 const p95 = trimmed[Math.min(trimmed.length - 1, Math.ceil(trimmed.length * 0.95) - 1)]
58
59 return {
60 median,
61 p95,
62 min: trimmed[0],
63 max: trimmed[trimmed.length - 1],
64 mean,
65 standardDeviation,
66 spreadPercent: median === 0 ? null : ((p95 - median) / Math.abs(median)) * 100,
67 sampleCount: sorted.length,
68 trimmedCount: trimmed.length,
69 }
70}
71
72function formatMs(value) {
73 return typeof value === 'number' ? `${value.toFixed(2)} ms` : '—'

Callers 1

aggregateVersionFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected