Merge pull request #786 from poolifier/worker-info
[poolifier.git] / src / utils.ts
index a6f5fbcc5b729b98f5dec1da1de8178eff830d40..7ac4ab25db67446a49b2fc42a0f219c043d4af6f 100644 (file)
@@ -32,7 +32,10 @@ export const DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS: MeasurementStatisticsR
   }
 
 /**
- * Safe helper to get the host OS optimized maximum pool size.
+ * Returns safe host OS optimized estimate of the default amount of parallelism a pool should use.
+ * Always returns a value greater than zero.
+ *
+ * @returns The host OS optimized maximum pool size.
  */
 export const availableParallelism = (): number => {
   let availableParallelism = 1
@@ -48,7 +51,7 @@ export const availableParallelism = (): number => {
 }
 
 /**
- * Compute the median of the given data set.
+ * Computes the median of the given data set.
  *
  * @param dataSet - Data set.
  * @returns The median of the given data set.
@@ -68,6 +71,19 @@ export const median = (dataSet: number[]): number => {
   )
 }
 
+/**
+ * Rounds the given number to the given scale.
+ * The rounding is done using the "round half away from zero" method.
+ *
+ * @param num - The number to round.
+ * @param scale - The scale to round to.
+ * @returns The rounded number.
+ */
+export const round = (num: number, scale = 2): number => {
+  const rounder = Math.pow(10, scale)
+  return Math.round(num * rounder * (1 + Number.EPSILON)) / rounder
+}
+
 /**
  * Is the given object a plain object?
  *