diff --git a/package.json b/package.json index 9d27aec7f..a612623e1 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "cheminfo-types": "^1.15.0", "fft.js": "^4.0.4", "is-any-array": "^3.0.0", - "ml-matrix": "^6.12.2", + "ml-matrix": "^6.13.0", "ml-xsadd": "^3.0.1" }, "devDependencies": { diff --git a/src/utils/fftCache.ts b/src/utils/fftCache.ts index acc4ff401..e81887ddd 100644 --- a/src/utils/fftCache.ts +++ b/src/utils/fftCache.ts @@ -9,7 +9,7 @@ class FFTCache { #maxSize: number; readonly #instances = new Map(); - constructor(maxSize = 10) { + constructor(maxSize = 1) { this.#maxSize = maxSize; } @@ -77,7 +77,7 @@ export function clearFFTCache(): void { /** * Sets the maximum number of distinct transform sizes kept in the shared FFT - * cache (default 10). When the cache is full and a new size is requested, the + * cache (default 1). When the cache is full and a new size is requested, the * whole cache is dropped and rebuilt on demand. Lowering the limit below the * current number of cached sizes clears the cache immediately. * @param maxSize - maximum number of cached sizes; must be a positive integer. diff --git a/src/xyArray/utils/getSlotsToFirst.ts b/src/xyArray/utils/getSlotsToFirst.ts index a0441bc66..cdd2a450c 100644 --- a/src/xyArray/utils/getSlotsToFirst.ts +++ b/src/xyArray/utils/getSlotsToFirst.ts @@ -20,6 +20,7 @@ export interface Slot { * Computes slots aligned to the first spectrum's x values, adding slots for any x values not covered. * @param data - data. * @param options - options. + * @returns the slots, sorted by x value, with non-overlapping `[from, to)` ranges. */ export function getSlotsToFirst( data: DataXY[], @@ -42,10 +43,15 @@ export function getSlotsToFirst( const otherXs = xyArrayWeightedMerge(data.slice(1), options).x; let currentPosition = 0; - for (const slot of slots) { + // Only the first-spectrum slots drive this pass; slots created for uncovered + // x values are appended and merged in by the sort below, so we freeze the + // count instead of iterating an array we are mutating. + const firstSlotCount = slots.length; + for (let i = 0; i < firstSlotCount; i++) { + const slot = slots[i]; while ( - otherXs[currentPosition] < slot.to && - currentPosition < otherXs.length + currentPosition < otherXs.length && + otherXs[currentPosition] < slot.to ) { if (otherXs[currentPosition] < slot.from) { const currentDelta = deltaIsFunction diff --git a/src/xyArray/xyArrayAlignToFirst.ts b/src/xyArray/xyArrayAlignToFirst.ts index fd3572446..b79a4efc4 100644 --- a/src/xyArray/xyArrayAlignToFirst.ts +++ b/src/xyArray/xyArrayAlignToFirst.ts @@ -16,6 +16,7 @@ export interface XYArrayAlignToFirstOptions { * If some x values are missing in the first spectrum we will add them * @param data - data * @param options - options + * @returns the common `x` axis and one aligned `y` array per input spectrum. */ export function xyArrayAlignToFirst( data: DataXY[], @@ -26,7 +27,10 @@ export function xyArrayAlignToFirst( } { const { delta = 1 } = options; const slots = getSlotsToFirst(data, { delta }); - const x = Float64Array.from(slots.map((slot) => slot.value)); + const x = new Float64Array(slots.length); + for (let i = 0; i < slots.length; i++) { + x[i] = slots[i].value; + } const ys = Array.from(data, () => new Float64Array(x.length)); const positions = new Uint32Array(data.length);