From: Jérôme Benoit Date: Mon, 8 May 2023 23:09:53 +0000 (+0200) Subject: perf: optimize median computation implementation X-Git-Tag: v2.4.14~7 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=f167044fd69fd0028160f3a92ad435c37be7de7b;p=poolifier.git perf: optimize median computation implementation Signed-off-by: Jérôme Benoit --- diff --git a/src/utils.ts b/src/utils.ts index 176d2c4f..da1459f9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -29,6 +29,9 @@ export const median = (dataSet: number[]): number => { return dataSet[0] } const sortedDataSet = dataSet.slice().sort((a, b) => a - b) + if (sortedDataSet.length % 2 === 0) { + return sortedDataSet[sortedDataSet.length / 2] + } return ( (sortedDataSet[(sortedDataSet.length - 1) >> 1] + sortedDataSet[sortedDataSet.length >> 1]) /