From: Jérôme Benoit Date: Sun, 3 Sep 2023 13:08:36 +0000 (+0200) Subject: perf: optimize min/max in hot code path X-Git-Tag: v2.6.41~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=68e7ed58fc1513e2e22bcfba2f78312266081519;p=poolifier.git perf: optimize min/max in hot code path Signed-off-by: Jérôme Benoit --- 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)