Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent Maximum call stack size exceeded #3816

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

baseballyama
Copy link

@baseballyama baseballyama commented Dec 20, 2024

close: #1740

Attempting to INSERT a large amount of data at once causes a Maximum call stack size exceeded error.
In my case, it occurred with the following:

result.params.push(...query.params);

The minimal code to reproduce this issue is as follows:

const array = [];
const items = Array.from({ length: 110088 }, (_, i) => i + 1);
array.push(...items)

To address this issue, I added a push_array utility to prevent the error.
Regarding performance, I believe the impact is negligible, but is there any benchmark data available to confirm this?

I’d like to add an ESLint rule to inspect the use of array.push(...items) in the future. What do you think? If you agree, I will create a separate PR for it.

Comment on lines +379 to +381
for (let i = 0; i < items.length; i++) {
array.push(items[i]!);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially faster implementation:

const oldLength = array.length;
array.length = array.length + items.length;
for (let i = oldLength; i < testArray.length; i++) {
  array[i] = items[i - oldLength];
}

In my testing it seems to be between 10-25% faster than the push method. Note that this doesn't have proper typescript ignore annotations or eslint disable annotations. I have tested this with arrays of 100-10,000,000 elements and the speedup seems to work across the board, even when items is much smaller than array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG]: RangeError: Maximum call stack size exceeded on insert of large amount of rows
2 participants