Skip to content

Commit 96984a7

Browse files
committed
simplify normalizeUnit function
1 parent 61ac5f5 commit 96984a7

File tree

1 file changed

+8
-23
lines changed

1 file changed

+8
-23
lines changed

src/normalizeUnit.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
export function normalizeUnit(prevUnit: string, currentUnit: string, value: number): number {
2-
const prevUnitIndex = TIME_UNITS.indexOf(prevUnit);
3-
const currentUnitIndex = TIME_UNITS.indexOf(currentUnit);
2+
for (const units of SUPPORTED_UNITS) {
3+
const prevUnitIndex = units.indexOf(prevUnit);
4+
const currentUnitIndex = units.indexOf(currentUnit);
45

5-
if (prevUnitIndex >= 0 && currentUnitIndex >= 0) {
6-
const unitDiff = prevUnitIndex - currentUnitIndex;
6+
if (prevUnitIndex >= 0 && currentUnitIndex >= 0) {
7+
const unitDiff = prevUnitIndex - currentUnitIndex;
78

8-
return value * 1000 ** unitDiff;
9-
}
10-
11-
const prevUnitIndex2 = ITER_UNITS.indexOf(prevUnit);
12-
const currentUnitIndex2 = ITER_UNITS.indexOf(currentUnit);
13-
14-
if (prevUnitIndex2 >= 0 && currentUnitIndex2 >= 0) {
15-
const unitDiff = prevUnitIndex2 - currentUnitIndex2;
16-
17-
return value * 1000 ** unitDiff;
18-
}
19-
20-
const prevUnitIndex3 = OPS_PER_TIME_UNIT.indexOf(prevUnit);
21-
const currentUnitIndex3 = OPS_PER_TIME_UNIT.indexOf(currentUnit);
22-
23-
if (prevUnitIndex3 >= 0 && currentUnitIndex3 >= 0) {
24-
const unitDiff = prevUnitIndex3 - currentUnitIndex3;
25-
26-
return value * 1000 ** unitDiff;
9+
return value * 1000 ** unitDiff;
10+
}
2711
}
2812

2913
return value;
@@ -32,3 +16,4 @@ export function normalizeUnit(prevUnit: string, currentUnit: string, value: numb
3216
const TIME_UNITS = ['s', 'ms', 'us', 'ns'];
3317
const ITER_UNITS = TIME_UNITS.map((unit) => `${unit}/iter`);
3418
const OPS_PER_TIME_UNIT = [...TIME_UNITS].reverse().map((unit) => `ops/${unit}`);
19+
const SUPPORTED_UNITS = [TIME_UNITS, ITER_UNITS, OPS_PER_TIME_UNIT];

0 commit comments

Comments
 (0)