perf: 2x performance gain preallocating the array

This commit is contained in:
aml360 2022-04-28 16:54:21 +02:00
parent a40cefc528
commit 9e1077269a
No known key found for this signature in database
GPG Key ID: 63961239476050D0

View File

@ -1,7 +1,7 @@
export default function rangeMap(n: number, fn: (i: number) => any) { export default function rangeMap(n: number, fn: (i: number) => any) {
const arr = [] const arr = new Array(n)
while (n > arr.length) { for (let i = 0; i < n; i++) {
arr.push(fn(arr.length)) arr[i] = fn(i)
} }
return arr return arr
} }