-
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathappend-to-array.mjs
More file actions
24 lines (21 loc) · 642 Bytes
/
append-to-array.mjs
File metadata and controls
24 lines (21 loc) · 642 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// @ts-check
const sliceSize = 1000;
/**
* Efficiently appends the source array to the destination array.
* @template T
* @param {T[]} destination Destination Array.
* @param {T[]} source Source Array.
* @returns {void}
*/
const appendToArray = (destination, source) => {
// NOTE: destination.push(...source) throws "RangeError: Maximum call stack
// size exceeded" for sufficiently lengthy source arrays
let index = 0;
let slice = null;
while ((slice = source.slice(index, index + sliceSize)).length > 0) {
destination.push(...slice);
index += sliceSize;
}
};
export default appendToArray;
export { sliceSize };