From 68e7ed58fc1513e2e22bcfba2f78312266081519 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 3 Sep 2023 15:08:36 +0200 Subject: [PATCH] perf: optimize min/max in hot code path MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index e864a015..ae4b8637 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -238,11 +238,11 @@ export const updateMeasurementStatistics = ( if (measurementRequirements.aggregate) { measurementStatistics.aggregate = (measurementStatistics.aggregate ?? 0) + measurementValue - measurementStatistics.minimum = Math.min( + measurementStatistics.minimum = min( measurementValue, measurementStatistics.minimum ?? Infinity ) - measurementStatistics.maximum = Math.max( + measurementStatistics.maximum = max( measurementValue, measurementStatistics.maximum ?? -Infinity ) @@ -273,3 +273,6 @@ export const updateMeasurementStatistics = ( export const secureRandom = (): number => { return webcrypto.getRandomValues(new Uint32Array(1))[0] / 0x100000000 } + +const min = (a: number, b: number): number => (a < b ? a : b) +const max = (a: number, b: number): number => (a > b ? a : b) -- 2.34.1