From 764608a21857125173b3ac09134fee8522dfde80 Mon Sep 17 00:00:00 2001 From: Luc Patiny Date: Thu, 3 Sep 2026 12:13:31 +0200 Subject: [PATCH] perf: optimize getSlots --- src/xyArray/utils/getSlots.ts | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/xyArray/utils/getSlots.ts b/src/xyArray/utils/getSlots.ts index c64f78340..4c181f121 100644 --- a/src/xyArray/utils/getSlots.ts +++ b/src/xyArray/utils/getSlots.ts @@ -28,10 +28,7 @@ export function getSlots( const { delta = 1 } = options; const deltaIsFunction = typeof delta === 'function'; - const possibleXs = Float64Array.from( - data.flatMap((spectrum) => spectrum.x as number[]), - ); - possibleXs.sort(); + const possibleXs = sortedXs(data); if (possibleXs.length === 0) { throw new Error('can not process empty arrays'); @@ -65,3 +62,24 @@ export function getSlots( } return slots; } + +/** + * Fast way of combining array using typed array and set + * @param data - The spectra. + * @returns Their x values, ascending. + */ +function sortedXs(data: DataXY[]): Float64Array { + let total = 0; + for (const spectrum of data) { + total += spectrum.x.length; + } + + const possibleXs = new Float64Array(total); + let at = 0; + for (const spectrum of data) { + possibleXs.set(spectrum.x, at); + at += spectrum.x.length; + } + possibleXs.sort(); + return possibleXs; +}