Skip to content

Commit

Permalink
Fix Max Call Stack Size Exceeded Issue
Browse files Browse the repository at this point in the history
* There can be issues with very large arrays when using `Array.push()` in conjunction with the spread operator. Specifically, it results in every element in the original array being pushed onto the call stack. Once the array gets huge, it will eventually result in the dreaded "Maximum call stack size exceeded" error. The fix for this is fairly simple: don't use the spread operator with `Array.push()`.
* To fix, I've simply replaced the spread operator with a simple `for` loop, which solves the issue.
  • Loading branch information
brianlenz authored and vladholubiev committed Jul 4, 2024
1 parent acf8ea8 commit f49c1c6
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/parallel-scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export async function parallelScan(
client: ddbClient,
});

totalItems.push(...segmentItems!);
for (const segmentItem of segmentItems) {
totalItems.push(segmentItem);
}
totalFetchedItemsCount += segmentItems!.length;
})
);
Expand Down Expand Up @@ -80,7 +82,9 @@ async function getItemsFromSegment(
ExclusiveStartKey = LastEvaluatedKey;
totalScannedItemsCount += ScannedCount!;

segmentItems.push(...Items!);
for (const item of Items) {
segmentItems.push(item);
}

debug(
`(${Math.round((totalScannedItemsCount / totalTableItemsCount) * 100)}%) ` +
Expand Down

0 comments on commit f49c1c6

Please sign in to comment.