Improve performance: reduce repeated DOM queries & heavy listeners#187
Merged
Conversation
|
@Aditya8369 is attempting to deploy a commit to the Om Roy's projects Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Changes Made
DocumentFragment Batching: Instead of appending each option button individually directly into the DOM (which triggers multiple reflows/rerenders), I modified the loadQuestion function in all the quiz JavaScript files to first append the options into a DocumentFragment. The complete fragment is then appended to the options container at the very end in a single operation.
Event Delegation: I removed the per-element onclick listeners being attached in the .forEach loop. Instead, I attached a single event listener to the parent #options container inside the DOMContentLoaded block. This leverages event bubbling (event delegation) to capture clicks on any child option button. This significantly reduces the memory overhead of having multiple listeners repeatedly added and removed when cycling through questions.
closes #169