Skip to content

Commit

Permalink
fix for maximum stack size exceeded exception when flattening large a…
Browse files Browse the repository at this point in the history
…rrays (#76)

* use array.concat instead of spread for array flattening

* 1.29.0

Co-authored-by: Shawn Jones <[email protected]>
  • Loading branch information
shawnjones253 and Shawn Jones authored Jul 11, 2022
1 parent a6ae8d1 commit 6a58d46
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blend-promise-utils",
"version": "1.28.0",
"version": "1.29.0",
"author": "Blend",
"license": "MIT",
"homepage": "https://blend.github.io/promise-utils",
Expand Down
4 changes: 2 additions & 2 deletions src/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ export async function flatMap(input: any, iteratee: any): Promise<any[]> {
if (!input) {
return [];
}
const output = [];
let output = [];
const nestedOutput = await map(input, iteratee);
for (const partialOutput of nestedOutput) {
// tslint:disable-next-line:no-any (could possibly be an array)
if (partialOutput && (partialOutput as any).length !== undefined) {
// tslint:disable-next-line:no-any (is definitely an array)
output.push(...(partialOutput as any));
output = output.concat(partialOutput) as any[];
} else {
output.push(partialOutput);
}
Expand Down
5 changes: 5 additions & 0 deletions test/flatMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ test('handles empty input group', async t => {
const output = await promiseUtils.flatMap([], _.identity);
t.deepEqual(output, []);
});

test('handles large sub-lists', async t => {
const output = await promiseUtils.flatMap(_.range(10), async () => _.range(1_000_000));
t.is(output.length, 10_000_000);
});

0 comments on commit 6a58d46

Please sign in to comment.