From: Jérôme Benoit Date: Mon, 8 May 2023 23:12:18 +0000 (+0200) Subject: perf: optimize median computation X-Git-Tag: v1.2.13~5 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=efc4c685062073ac9eddecabb10910fa6d09b438;p=e-mobility-charging-stations-simulator.git perf: optimize median computation Signed-off-by: Jérôme Benoit --- diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index be372de6..f1d098cc 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -344,6 +344,9 @@ export class Utils { 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]) / 2