Lessons in this course (10)
9. Performance and best practices
Intermediate★ Must knowLesson 9 of 10
- Cache selectors (
const $list = $("#list")) instead of re-querying in loops. - Use ID selectors or scope searches with
.find(); avoid jQuery-only pseudo-selectors (:visible,:even) on large sets. - Batch DOM changes — build an HTML string/fragment and append once instead of appending in a loop.
- Use event delegation for lists and tables.
- Use
.text()and$("<tag>", { text })for user content (XSS-safe); avoid.html()with untrusted data; keep jQuery ≥ 3.5. - Prefer CSS classes over many
.css()calls. - Detach elements (
.detach()) before heavy manipulation, then re-insert. - Use
$.noConflict()if another library uses$.