Performance: Flash messages
Flash messages affect page load speed and user interaction responsiveness by adding DOM elements and triggering reflows when shown or hidden.
Render flash messages conditionally only when present, and use CSS transitions with opacity for showing/hiding without layout changes.
In the layout, always render flash divs with content even if empty, and use JavaScript to show/hide them by toggling display styles on every page load.
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Always render flash divs and toggle display | High (extra nodes always present) | Multiple reflows per page load | High (layout changes trigger paint) | [X] Bad |
| Render flash divs conditionally and use opacity transitions | Low (only when needed) | Single reflow on insertion | Low (opacity changes only trigger paint/composite) | [OK] Good |
def create
if @post.save
flash[:notice] = "Post created!"
redirect_to posts_path
else
flash[:alert] = "Error creating post"
render :new
end
enddef update
if @user.update(user_params)
flash[:notice] = "Profile updated"
render redirect_to profile_path
else
flash[:alert] = "Update failed"
redirect_to edit_profile_path
end
endPass the test and this pattern moves unknown