-
Notifications
You must be signed in to change notification settings - Fork 0
β‘ Bolt: [performance improvement] #11
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
Open
praxstack
wants to merge
1
commit into
main
Choose a base branch
from
bolt/optimize-recursive-dom-rendering-12786131008443236953
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+95
β35
Open
Changes from all commits
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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,9 @@ | ||
| ## 2024-05-24 - [Avoid `forEach` overhead in recursive codebase methods] | ||
|
|
||
| **Learning:** In highly recursive file and directory scanning processes (e.g., generating file tree structures), the repeated array `.forEach` calls generate unnecessary function closures and allocations which can slow down applications significantly. This overhead becomes apparent when browsing large nested markdown folders. | ||
| **Action:** Replace `Array.prototype.forEach` with `for...of` statements in code paths related to file/folder traversal or recursive processes (like `FolderBrowserService.js` `getAllFiles` and `countFiles`). This maintains readability while avoiding the closure creation overhead, and should be the preferred pattern when processing unbounded or deep file trees. | ||
| **Action:** Replace `Array.prototype.forEach` with `for...of` statements in code paths related to file/folder traversal or recursive processes (like `FolderBrowserService.js` `getAllFiles` and `countFiles`). This maintains readability while avoiding the closure creation overhead, and should be the preferred pattern when processing unbounded or deep file trees. | ||
|
|
||
| ## 2024-05-25 - [Optimize recursive DOM rendering with DocumentFragment and for...of] | ||
|
|
||
| **Learning:** In recursive UI rendering processes (e.g., `renderFileTree` or `populateLocationDropdown`), using `.forEach` leads to excessive closure allocations. Appending elements directly to the live DOM inside recursive loops causes severe layout thrashing. The combination of both leads to unacceptable UI lockup during deep folder expansion. | ||
| **Action:** Always combine `for...of` loops with `document.createDocumentFragment()` to batch DOM insertions before appending them to the container in recursive Vanilla JS DOM manipulation. This prevents both closure allocation bottlenecks and synchronous layout thrashing. |
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
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
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
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.
Potential issue: DocumentFragment is appended mid-loop before all items are added.
The current implementation appends the fragment to
newFileLocationSelecton line 2358 before recursing, then appends it again at line 2365. This pattern has issues:appendChild(fragment), the fragment becomes empty (its children move to the DOM)appendChild(fragment)on line 2365 only appends items added after the mid-loop appendThis may cause directories without children to appear after their sibling directories that have children, breaking the expected hierarchical order.
π Proposed fix: Append fragment only once at the end of each recursive call
function addDirectories(items, path = '', indent = 0) { // β‘ Bolt Optimization: Use DocumentFragment to batch DOM insertions const fragment = document.createDocumentFragment(); // β‘ Bolt Optimization: Use for...of instead of .forEach to avoid closure allocation for (const item of items) { if (item.type === 'directory') { const fullPath = path ? `${path}/${item.name}` : item.name; const option = document.createElement('option'); option.value = fullPath; option.textContent = `${' '.repeat(indent)}π ${item.name}`; fragment.appendChild(option); - - if (item.children) { - // Append children directly to the select element for simplicity, - // or we could return a fragment. Given the recursive nature, - // appending the current fragment then calling recursively is easiest. - newFileLocationSelect.appendChild(fragment); - addDirectories(item.children, fullPath, indent + 1); - } } } - - // Append any remaining items in the fragment newFileLocationSelect.appendChild(fragment); + + // Recurse into children after appending current level + for (const item of items) { + if (item.type === 'directory' && item.children) { + const fullPath = path ? `${path}/${item.name}` : item.name; + addDirectories(item.children, fullPath, indent + 1); + } + } }π€ Prompt for AI Agents