Perf: append large array

This commit is contained in:
SukkaW
2024-04-28 14:20:36 +08:00
parent 16746057e1
commit 1c95540b81
3 changed files with 26 additions and 2 deletions

View File

@@ -0,0 +1,21 @@
const MAX_BLOCK_SIZE = 65535; // max parameter array size for use in Webkit
export function appendArrayInPlace<T>(dest: T[], source: T[]) {
let offset = 0;
let itemsLeft = source.length;
if (itemsLeft <= MAX_BLOCK_SIZE) {
// eslint-disable-next-line prefer-spread -- performance
dest.push.apply(dest, source);
} else {
while (itemsLeft > 0) {
const pushCount = Math.min(MAX_BLOCK_SIZE, itemsLeft);
const subSource = source.slice(offset, offset + pushCount);
// eslint-disable-next-line prefer-spread -- performance
dest.push.apply(dest, subSource);
itemsLeft -= pushCount;
offset += pushCount;
}
}
return dest;
}