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