feat: add statistics accounting to ELU fields
[poolifier.git] / src / utils.ts
1 import type { WorkerChoiceStrategyOptions } from './pools/selection-strategies/selection-strategies-types'
2
3 /**
4 * An intentional empty function.
5 */
6 export const EMPTY_FUNCTION: () => void = Object.freeze(() => {
7 /* Intentionally empty */
8 })
9
10 /**
11 * Default worker choice strategy options.
12 */
13 export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS: WorkerChoiceStrategyOptions =
14 {
15 runTime: { median: false },
16 waitTime: { median: false },
17 elu: { median: false }
18 }
19
20 /**
21 * Compute the median of the given data set.
22 *
23 * @param dataSet - Data set.
24 * @returns The median of the given data set.
25 */
26 export const median = (dataSet: number[]): number => {
27 if (Array.isArray(dataSet) && dataSet.length === 0) {
28 return 0
29 }
30 if (Array.isArray(dataSet) && dataSet.length === 1) {
31 return dataSet[0]
32 }
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 )
39 }
40
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 */
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]'