Logical concatenation for large arrays, in TypeScript (for copy-n-paste / no library):
- Chain and process large arrays, without making any copies, for efficient memory use.
Just copy chain-arrays.ts (or chain-arrays.js) file into your project, and you're good to go 🚀
Functions chainArrays and chainArraysReverse there are self-explanatory 😉
When dealing with large arrays (10^6 > elements), concatenating them can be very memory-consuming. This solution joins arrays logically, turning a list of arrays into an Iterable, plus getLength and at functions.
import {chainArrays, chainArraysReverse} from './chain-arrays'; const a = [1, 2]; const b = [3, 4]; const c = [5, 6]; for (const value of chainArrays(a, b, c)) { console.log(value); //=> 1, 2, 3, 4, 5, 6 } for (const value of chainArraysReverse(a, b, c)) { console.log(value); //=> 6, 5, 4, 3, 2, 1 }import {chainArrays} from './chain-arrays'; const r = 10_000_000; const a = Array<number>(r).fill(1); const b = Array<number>(r).fill(2); const c = Array<number>(r).fill(3); const d = Array<number>(r).fill(4); const e = Array<number>(r).fill(5); const start = Date.now(); let sum = 0; for (const i of chainArrays(a, b, c, d, e)) { sum += i; } console.log(`${Date.now() - start}ms`); //=> ~100msAbove, we iterate over 5 arrays, with 10 mln elements each, within 100ms.
For comparison, using the spread syntax for the same:
let sum = 0; for (const i of [...a, ...b, ...c, ...d, ...e]) { sum += i; } console.log(`${Date.now() - start}ms`); //=> ~1175msThat took 11.75 times longer, while also consuming tremendously more memory.
The same iteration via index is roughly 2 times slower, as it needs to calculate the source array index every time you use at function:
let sum = 0; const {at, getLength} = chainArrays(a, b, c, d, e); const length = getLength(); for (let t = 0; t < length; t++) { sum += at(t)!; } console.log(`${Date.now() - start}ms`); //=> ~190ms