-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Resolve the memory leak problem when creating multiple HLJS instances (#4086) #4095
Merged
+14
−10
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the thinking here? Is this type of cleanup truly necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cleanup might only matter in rare cases, like when an
hljs
instance is used just once andhighlightAll
is called beforeDOMContentLoaded
. In such a scenario, the instance would stay in memory, unable to be released. If this corner case isn't a concern, the cleanup is likely unnecessary.If this contradicts the core philosophy, the cleanup can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just going to call it unnecessary -- unless you have some docs (from MDN or such) that says this is somehow best practice... I've seen tons of
onload
hooks like this over the years but never code to unwind a single-use event listner afterwards - seems a browser concern... if the browser knows that event that just fired can never be fired again on the same page it could do the cleanup itself.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand your point. While the event could technically be triggered using
window.dispatchEvent(new Event("DOMContentLoaded"))
, which isn't common practice, this means the browser wouldn’t automatically handle the cleanup. However, another approach to ensure the listener is only triggered once and automatically cleaned up is to usewindow.addEventListener(..., { once: true })
. Is this one better, or just remove the cleanup?Would you prefer switching to
{ once: true }
, or should we remove the cleanup entirely?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://developer.mozilla.org/en-US/docs/Web/API/Document/DOMContentLoaded_event
I find zero examples of
once
being suggested. Lets just remove the cleanup.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clarification and patience! I've removed the cleanup.