Merge pull request #785 from poolifier/dependabot/npm_and_yarn/eslint-plugin-jsdoc...
[poolifier.git] / src / utils.ts
... / ...
CommitLineData
1import type {
2 MeasurementStatisticsRequirements,
3 WorkerChoiceStrategyOptions
4} from './pools/selection-strategies/selection-strategies-types'
5
6/**
7 * An intentional empty function.
8 */
9export const EMPTY_FUNCTION: () => void = Object.freeze(() => {
10 /* Intentionally empty */
11})
12
13/**
14 * Default worker choice strategy options.
15 */
16export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS: WorkerChoiceStrategyOptions =
17 {
18 runTime: { median: false },
19 waitTime: { median: false },
20 elu: { median: false }
21 }
22
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
33/**
34 * Compute the median of the given data set.
35 *
36 * @param dataSet - Data set.
37 * @returns The median of the given data set.
38 */
39export const median = (dataSet: number[]): number => {
40 if (Array.isArray(dataSet) && dataSet.length === 0) {
41 return 0
42 }
43 if (Array.isArray(dataSet) && dataSet.length === 1) {
44 return dataSet[0]
45 }
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 )
52}
53
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 */
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]'