1 import os from
'node:os'
3 MeasurementStatisticsRequirements
,
4 WorkerChoiceStrategyOptions
5 } from
'./pools/selection-strategies/selection-strategies-types'
6 import type { KillBehavior
} from
'./worker/worker-options'
9 * An intentional empty function.
11 export const EMPTY_FUNCTION
: () => void = Object.freeze(() => {
12 /* Intentionally empty */
16 * Default worker choice strategy options.
18 export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
: WorkerChoiceStrategyOptions
=
20 runTime
: { median
: false },
21 waitTime
: { median
: false },
22 elu
: { median
: false }
26 * Default measurement statistics requirements.
28 export const DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
: MeasurementStatisticsRequirements
=
36 * Returns safe host OS optimized estimate of the default amount of parallelism a pool should use.
37 * Always returns a value greater than zero.
39 * @returns The host OS optimized maximum pool size.
41 export const availableParallelism
= (): number => {
42 let availableParallelism
= 1
44 availableParallelism
= os
.availableParallelism()
46 const cpus
= os
.cpus()
47 if (Array.isArray(cpus
) && cpus
.length
> 0) {
48 availableParallelism
= cpus
.length
51 return availableParallelism
55 * Computes the median of the given data set.
57 * @param dataSet - Data set.
58 * @returns The median of the given data set.
60 export const median
= (dataSet
: number[]): number => {
61 if (Array.isArray(dataSet
) && dataSet
.length
=== 0) {
64 if (Array.isArray(dataSet
) && dataSet
.length
=== 1) {
67 const sortedDataSet
= dataSet
.slice().sort((a
, b
) => a
- b
)
69 (sortedDataSet
[(sortedDataSet
.length
- 1) >> 1] +
70 sortedDataSet
[sortedDataSet
.length
>> 1]) /
76 * Rounds the given number to the given scale.
77 * The rounding is done using the "round half away from zero" method.
79 * @param num - The number to round.
80 * @param scale - The scale to round to.
81 * @returns The rounded number.
83 export const round
= (num
: number, scale
= 2): number => {
84 const rounder
= Math.pow(10, scale
)
85 return Math.round(num
* rounder
* (1 + Number.EPSILON
)) / rounder
89 * Is the given object a plain object?
91 * @param obj - The object to check.
92 * @returns `true` if the given object is a plain object, `false` otherwise.
94 export const isPlainObject
= (obj
: unknown
): boolean =>
95 typeof obj
=== 'object' &&
97 obj
?.constructor
=== Object &&
98 Object.prototype
.toString
.call(obj
) === '[object Object]'
101 * Detects whether the given value is a kill behavior or not.
103 * @typeParam KB - Which specific KillBehavior type to test against.
104 * @param killBehavior - Which kind of kill behavior to detect.
105 * @param value - Any value.
106 * @returns `true` if `value` was strictly equals to `killBehavior`, otherwise `false`.
108 export const isKillBehavior
= <KB
extends KillBehavior
>(
112 return value
=== killBehavior