refactor: remove unneeded array building in median computation
[poolifier.git] / src / utils.ts
... / ...
CommitLineData
1import type { WorkerChoiceStrategyOptions } from './pools/selection-strategies/selection-strategies-types'
2
3/**
4 * An intentional empty function.
5 */
6export const EMPTY_FUNCTION: () => void = Object.freeze(() => {
7 /* Intentionally empty */
8})
9
10/**
11 * Default worker choice strategy options.
12 */
13export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS: WorkerChoiceStrategyOptions =
14 {
15 medRunTime: false
16 }
17
18/**
19 * Compute the median of the given data set.
20 *
21 * @param dataSet - Data set.
22 * @returns The median of the given data set.
23 */
24export const median = (dataSet: number[]): number => {
25 if (Array.isArray(dataSet) && dataSet.length === 1) {
26 return dataSet[0]
27 }
28 dataSet = dataSet.slice().sort((a, b) => a - b)
29 return (dataSet[(dataSet.length - 1) >> 1] + dataSet[dataSet.length >> 1]) / 2
30}
31
32export const isPlainObject = (obj: unknown): boolean =>
33 typeof obj === 'object' &&
34 obj !== null &&
35 obj?.constructor === Object &&
36 Object.prototype.toString.call(obj) === '[object Object]'