1 import os from
'node:os'
3 MeasurementStatisticsRequirements
,
4 WorkerChoiceStrategyOptions
5 } from
'./pools/selection-strategies/selection-strategies-types'
8 * An intentional empty function.
10 export const EMPTY_FUNCTION
: () => void = Object.freeze(() => {
11 /* Intentionally empty */
15 * Default worker choice strategy options.
17 export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
: WorkerChoiceStrategyOptions
=
19 runTime
: { median
: false },
20 waitTime
: { median
: false },
21 elu
: { median
: false }
25 * Default measurement statistics requirements.
27 export const DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
: MeasurementStatisticsRequirements
=
35 * Returns safe host OS optimized estimate of the default amount of parallelism a pool should use.
36 * Always returns a value greater than zero.
38 * @returns The host OS optimized maximum pool size.
40 export const availableParallelism
= (): number => {
41 let availableParallelism
= 1
43 availableParallelism
= os
.availableParallelism()
45 const cpus
= os
.cpus()
46 if (Array.isArray(cpus
) && cpus
.length
> 0) {
47 availableParallelism
= cpus
.length
50 return availableParallelism
54 * Computes the median of the given data set.
56 * @param dataSet - Data set.
57 * @returns The median of the given data set.
59 export const median
= (dataSet
: number[]): number => {
60 if (Array.isArray(dataSet
) && dataSet
.length
=== 0) {
63 if (Array.isArray(dataSet
) && dataSet
.length
=== 1) {
66 const sortedDataSet
= dataSet
.slice().sort((a
, b
) => a
- b
)
68 (sortedDataSet
[(sortedDataSet
.length
- 1) >> 1] +
69 sortedDataSet
[sortedDataSet
.length
>> 1]) /
75 * Rounds the given number to the given scale.
76 * The rounding is done using the "round half away from zero" method.
78 * @param num - The number to round.
79 * @param scale - The scale to round to.
80 * @returns The rounded number.
82 export const round
= (num
: number, scale
= 2): number => {
83 const rounder
= Math.pow(10, scale
)
84 return Math.round(num
* rounder
* (1 + Number.EPSILON
)) / rounder
88 * Is the given object a plain object?
90 * @param obj - The object to check.
91 * @returns `true` if the given object is a plain object, `false` otherwise.
93 export const isPlainObject
= (obj
: unknown
): boolean =>
94 typeof obj
=== 'object' &&
96 obj
?.constructor
=== Object &&
97 Object.prototype
.toString
.call(obj
) === '[object Object]'