-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathnormalizeBenchmarkResult.ts
More file actions
33 lines (29 loc) · 1.23 KB
/
normalizeBenchmarkResult.ts
File metadata and controls
33 lines (29 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { BenchmarkResult } from './extract';
import { normalizeValueByUnit } from './normalizeValueByUnit';
import { extractRangeInfo } from './extractRangeInfo';
export function normalizeBenchmarkResult(
prevBenchResult: BenchmarkResult | undefined | null,
currentBenchResult: BenchmarkResult,
): BenchmarkResult {
if (!prevBenchResult) {
return currentBenchResult;
}
const prevUnit = prevBenchResult.unit;
const currentUnit = currentBenchResult.unit;
const currentRange = currentBenchResult.range;
const currentRangeInfo = extractRangeInfo(currentRange);
const normalizedValue = normalizeValueByUnit(prevUnit, currentUnit, currentBenchResult.value);
const normalizedUnit = currentBenchResult.value !== normalizedValue ? prevUnit : currentUnit;
const normalizedRangeInfo = currentRangeInfo
? {
prefix: currentRangeInfo.prefix,
value: normalizeValueByUnit(prevUnit, currentUnit, currentRangeInfo.value),
}
: undefined;
return {
...currentBenchResult,
value: normalizedValue,
unit: normalizedUnit,
range: normalizedRangeInfo ? `${normalizedRangeInfo.prefix}${normalizedRangeInfo.value}` : currentRange,
};
}