perf: generalize optimized min/max
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 3 Sep 2023 13:48:20 +0000 (15:48 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 3 Sep 2023 13:48:20 +0000 (15:48 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts
src/utils.ts
tests/utils.test.js

index ab7fcf518ed781596a74bb953150fa02f05cb6f0..6668361f9491a7cf927fba8734a11c37781a3b87 100644 (file)
@@ -14,7 +14,9 @@ import {
   average,
   isKillBehavior,
   isPlainObject,
+  max,
   median,
+  min,
   round,
   updateMeasurementStatistics
 } from '../utils'
@@ -414,14 +416,14 @@ export abstract class AbstractPool<
         .runTime.aggregate && {
         runTime: {
           minimum: round(
-            Math.min(
+            min(
               ...this.workerNodes.map(
                 (workerNode) => workerNode.usage.runTime?.minimum ?? Infinity
               )
             )
           ),
           maximum: round(
-            Math.max(
+            max(
               ...this.workerNodes.map(
                 (workerNode) => workerNode.usage.runTime?.maximum ?? -Infinity
               )
@@ -457,14 +459,14 @@ export abstract class AbstractPool<
         .waitTime.aggregate && {
         waitTime: {
           minimum: round(
-            Math.min(
+            min(
               ...this.workerNodes.map(
                 (workerNode) => workerNode.usage.waitTime?.minimum ?? Infinity
               )
             )
           ),
           maximum: round(
-            Math.max(
+            max(
               ...this.workerNodes.map(
                 (workerNode) => workerNode.usage.waitTime?.maximum ?? -Infinity
               )
index ae4b8637f95821ada8d6ec65019e48d342c2a139..21cf3169b73040967bd856bc69e4a311cede6bf5 100644 (file)
@@ -274,5 +274,8 @@ 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)
+export const min = (...args: number[]): number =>
+  args.reduce((minimum, num) => (minimum < num ? minimum : num), Infinity)
+
+export const max = (...args: number[]): number =>
+  args.reduce((maximum, num) => (maximum > num ? maximum : num), -Infinity)
index 93e9e5c2e9b17f99165beb29e028239c7328dfa9..2101891a3982e575ecec3760a3d25b34e196f5e1 100644 (file)
@@ -20,7 +20,9 @@ const {
   isAsyncFunction,
   isKillBehavior,
   isPlainObject,
+  max,
   median,
+  min,
   round,
   secureRandom,
   sleep,
@@ -311,4 +313,16 @@ describe('Utils test suite', () => {
     expect(randomNumber).toBeGreaterThanOrEqual(0)
     expect(randomNumber).toBeLessThan(1)
   })
+
+  it('Verify min() behavior', () => {
+    expect(min(1, 2)).toBe(1)
+    expect(min(2, 1)).toBe(1)
+    expect(min(1, 1)).toBe(1)
+  })
+
+  it('Verify max() behavior', () => {
+    expect(max(1, 2)).toBe(2)
+    expect(max(2, 1)).toBe(2)
+    expect(max(1, 1)).toBe(1)
+  })
 })