Skip to content

Commit

Permalink
Optimize maybeHideFeedback for initial page render (#2953)
Browse files Browse the repository at this point in the history
* Optimize maybeHideFeedback for initial page render

This PR optimizes the initial page render for pages containing thousands of inputs; instead of calling `DOM.all` for each input, we build a selector to match all inputs and query the DOM.

From my testing, this has reduced the page rendering time from over 30 seconds to 4-5 seconds.

* leverage feedbacks to accumulate selector
  • Loading branch information
Schultzer authored Dec 18, 2023
1 parent 744a3ab commit 297a782
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 8 additions & 4 deletions assets/js/phoenix_live_view/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,14 @@ let DOM = {
}
},

maybeHideFeedback(container, input, phxFeedbackFor){
if(!(this.private(input, PHX_HAS_FOCUSED) || this.private(input, PHX_HAS_SUBMITTED))){
let feedbacks = [input.name]
if(input.name.endsWith("[]")){ feedbacks.push(input.name.slice(0, -2)) }
maybeHideFeedback(container, inputs, phxFeedbackFor){
let feedbacks = []
inputs.forEach(input => {
if(!(this.private(input, PHX_HAS_FOCUSED) || this.private(input, PHX_HAS_SUBMITTED))){
input.name.endsWith("[]") ? feedbacks.push(input.name.slice(0, -2)) : feedbacks.push(input.name)
}
})
if (feedbacks.length > 0) {
let selector = feedbacks.map(f => `[${phxFeedbackFor}="${f}"]`).join(", ")
DOM.all(container, selector, el => el.classList.add(PHX_NO_FEEDBACK_CLASS))
}
Expand Down
4 changes: 1 addition & 3 deletions assets/js/phoenix_live_view/dom_patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,7 @@ export default class DOMPatch {
})
}

trackedInputs.forEach(input => {
DOM.maybeHideFeedback(targetContainer, input, phxFeedbackFor)
})
DOM.maybeHideFeedback(targetContainer, trackedInputs, phxFeedbackFor)

liveSocket.silenceEvents(() => DOM.restoreFocus(focused, selectionStart, selectionEnd))
DOM.dispatchEvent(document, "phx:update")
Expand Down

0 comments on commit 297a782

Please sign in to comment.