Commit | Line | Data |
---|---|---|
bbeadd16 JB |
1 | import type { WorkerChoiceStrategyOptions } from './pools/selection-strategies/selection-strategies-types' |
2 | ||
6e9d10db JB |
3 | /** |
4 | * An intentional empty function. | |
5 | */ | |
4f3c3d89 | 6 | export const EMPTY_FUNCTION: () => void = Object.freeze(() => { |
6e9d10db | 7 | /* Intentionally empty */ |
4f3c3d89 | 8 | }) |
78099a15 JB |
9 | |
10 | /** | |
bbeadd16 JB |
11 | * Default worker choice strategy options. |
12 | */ | |
13 | export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS: WorkerChoiceStrategyOptions = | |
14 | { | |
932fc8be | 15 | runTime: { median: false }, |
5df69fab JB |
16 | waitTime: { median: false }, |
17 | elu: { median: false } | |
bbeadd16 JB |
18 | } |
19 | ||
20 | /** | |
21 | * Compute the median of the given data set. | |
78099a15 JB |
22 | * |
23 | * @param dataSet - Data set. | |
24 | * @returns The median of the given data set. | |
25 | */ | |
26 | export const median = (dataSet: number[]): number => { | |
4a45e8d2 JB |
27 | if (Array.isArray(dataSet) && dataSet.length === 0) { |
28 | return 0 | |
29 | } | |
78099a15 JB |
30 | if (Array.isArray(dataSet) && dataSet.length === 1) { |
31 | return dataSet[0] | |
32 | } | |
c6f42dd6 JB |
33 | const sortedDataSet = dataSet.slice().sort((a, b) => a - b) |
34 | return ( | |
35 | (sortedDataSet[(sortedDataSet.length - 1) >> 1] + | |
36 | sortedDataSet[sortedDataSet.length >> 1]) / | |
37 | 2 | |
38 | ) | |
78099a15 | 39 | } |
0d80593b | 40 | |
3c653a03 JB |
41 | /** |
42 | * Is the given object a plain object? | |
43 | * | |
44 | * @param obj - The object to check. | |
45 | * @returns `true` if the given object is a plain object, `false` otherwise. | |
46 | */ | |
0d80593b JB |
47 | export const isPlainObject = (obj: unknown): boolean => |
48 | typeof obj === 'object' && | |
49 | obj !== null && | |
50 | obj?.constructor === Object && | |
51 | Object.prototype.toString.call(obj) === '[object Object]' |