Skip to content

Commit

Permalink
use loop over <array-a>.push(...<array-b>)
Browse files Browse the repository at this point in the history
Prevent `Maximum call stack size exceeded` exception when
`<array-b>` is large.
  • Loading branch information
christiantakle authored and gcanti committed Mar 25, 2024
1 parent 704e7ce commit 693220a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,10 @@ export const chainWithIndex =
(as: Array<A>): Array<B> => {
const out: Array<B> = []
for (let i = 0; i < as.length; i++) {
out.push(...f(i, as[i]))
const bs = f(i, as[i])
for (let j = 0; j < bs.length; j++) {
out.push(bs[j])
}
}
return out
}
Expand Down
5 changes: 4 additions & 1 deletion src/NonEmptyArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,10 @@ export const chainWithIndex =
(as: NonEmptyArray<A>): NonEmptyArray<B> => {
const out: NonEmptyArray<B> = fromReadonlyNonEmptyArray(f(0, head(as)))
for (let i = 1; i < as.length; i++) {
out.push(...f(i, as[i]))
const bs = f(i, as[i])
for (let j = 0; j < bs.length; j++) {
out.push(bs[j])
}
}
return out
}
Expand Down
5 changes: 4 additions & 1 deletion src/ReadonlyArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,10 @@ export const chainWithIndex =
}
const out: Array<B> = []
for (let i = 0; i < as.length; i++) {
out.push(...f(i, as[i]))
const bs = f(i, as[i])
for (let j = 0; j < bs.length; j++) {
out.push(bs[j])
}
}
return out
}
Expand Down
5 changes: 4 additions & 1 deletion src/ReadonlyNonEmptyArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,10 @@ export const chainWithIndex =
(as: ReadonlyNonEmptyArray<A>): ReadonlyNonEmptyArray<B> => {
const out: NonEmptyArray<B> = _.fromReadonlyNonEmptyArray(f(0, head(as)))
for (let i = 1; i < as.length; i++) {
out.push(...f(i, as[i]))
const bs = f(i, as[i])
for (let j = 0; j < bs.length; j++) {
out.push(bs[j])
}
}
return out
}
Expand Down

0 comments on commit 693220a

Please sign in to comment.