Merge branch 'master' into interleaved-weighted-round-robin-worker-choice-strategy
[poolifier.git] / src / utils.ts
index e56c058e5c99a292d1ac2028290ddb2c3f19752f..176d2c4f875fb2b3fc695ad419a8398d12467bb5 100644 (file)
@@ -22,11 +22,18 @@ export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS: WorkerChoiceStrategyOptions
  * @returns The median of the given data set.
  */
 export const median = (dataSet: number[]): number => {
+  if (Array.isArray(dataSet) && dataSet.length === 0) {
+    return 0
+  }
   if (Array.isArray(dataSet) && dataSet.length === 1) {
     return dataSet[0]
   }
-  dataSet = [...dataSet].slice().sort((a, b) => a - b)
-  return (dataSet[(dataSet.length - 1) >> 1] + dataSet[dataSet.length >> 1]) / 2
+  const sortedDataSet = dataSet.slice().sort((a, b) => a - b)
+  return (
+    (sortedDataSet[(sortedDataSet.length - 1) >> 1] +
+      sortedDataSet[sortedDataSet.length >> 1]) /
+    2
+  )
 }
 
 export const isPlainObject = (obj: unknown): boolean =>