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

refactor: use for loop replace spread to fix stack overflow #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions dev/lib/edit-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,16 @@ export class EditMap {
events.length = this.map[index][0]
}

vecs.push([...events])
vecs.push(events.slice())
events.length = 0

let slice = vecs.pop()

while (slice) {
events.push(...slice)
// NOTE: do not use spread operator here, it will cause a stack overflow.
Copy link
Member

Choose a reason for hiding this comment

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

Better than a comment, would be a test that fails with the current logic and passes after

Copy link
Author

Choose a reason for hiding this comment

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

I add a large markdown file to test/fixtures, it will fail when run the original codes.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, could we remove this comment now, since the test will cover it?

for (let i = 0; i < slice.length; i++) {
Copy link
Member

Choose a reason for hiding this comment

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

Adding a loop could could significant negative performance impact.
Could concat be used instead?

See:
micromark/micromark#187
micromark/micromark@acc135e

Copy link
Author

Choose a reason for hiding this comment

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

I try to use Array.contact to replace for loop, but the event is a parameter, I just can modify it in-place, do you have any recommend, thx.

events.push(slice[i])
}
slice = vecs.pop()
}

Expand Down
Loading