From efc4c685062073ac9eddecabb10910fa6d09b438 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 9 May 2023 01:12:18 +0200 Subject: [PATCH] perf: optimize median computation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils/Utils.ts | 3 +++ 1 file changed, 3 insertions(+) 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 -- 2.34.1