Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
frontend ruleP1universalStack: javascript
performancedomreadability
Cache repeated DOM query results in a variable
Store the result of a DOM/selector query in a variable instead of re-querying the same element repeatedly.
PR: hegnar-web · org-mining-hist-2026-06Created: Jun 19, 2026
Bad example
Old codejavascript
| 1 | $(el).addClass('active'); |
| 2 | $(el).attr('data-id', id); |
| 3 | $(el).find('.label').text(name); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codejavascript
| 1 | const $element = $(el); |
| 2 | $element.addClass('active'); |
| 3 | $element.attr('data-id', id); |
| 4 | $element.find('.label').text(name); |
Explanation (EN)
Objašnjenje (HR)