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 * Safe helper to get the host OS optimized maximum pool size.
37 export const availableParallelism
= (): number => {
38 let availableParallelism
= 1
40 availableParallelism
= os
.availableParallelism()
42 const cpus
= os
.cpus()
43 if (Array.isArray(cpus
) && cpus
.length
> 0) {
44 availableParallelism
= cpus
.length
47 return availableParallelism
51 * Compute the median of the given data set.
53 * @param dataSet - Data set.
54 * @returns The median of the given data set.
56 export const median
= (dataSet
: number[]): number => {
57 if (Array.isArray(dataSet
) && dataSet
.length
=== 0) {
60 if (Array.isArray(dataSet
) && dataSet
.length
=== 1) {
63 const sortedDataSet
= dataSet
.slice().sort((a
, b
) => a
- b
)
65 (sortedDataSet
[(sortedDataSet
.length
- 1) >> 1] +
66 sortedDataSet
[sortedDataSet
.length
>> 1]) /
72 * Is the given object a plain object?
74 * @param obj - The object to check.
75 * @returns `true` if the given object is a plain object, `false` otherwise.
77 export const isPlainObject
= (obj
: unknown
): boolean =>
78 typeof obj
=== 'object' &&
80 obj
?.constructor
=== Object &&
81 Object.prototype
.toString
.call(obj
) === '[object Object]'