jQuery interview question · Most asked
How do you improve the performance of jQuery-heavy pages?
Summary
Cache selections, scope queries, use event delegation instead of thousands of handlers, batch DOM changes (build off-DOM, insert once), avoid layout thrashing (separate reads such as .height() from writes), debounce or throttle scroll and resize handlers, prefer CSS transitions, and load only the plugins you use.
Longer Answer
For large manipulations, .detach() an element, modify it, and reattach it to avoid repeated reflows. Measure with the browser's Performance panel: long tasks from jQuery code usually come from repeated querying in loops, inserting elements one by one, or synchronous AJAX. Upgrading old jQuery versions also brings performance improvements.
Key Points
- Cache and scope selectors.
- Delegate events.
- Batch DOM writes; avoid thrashing.
- Debounce scroll/resize.
More most-asked jQuery questions
- How do you make AJAX requests with jQuery?
- What is the difference between .html() and .text(), and why does it matter for security?
- What is event delegation, and why is it useful?
- What is the difference between $(document).ready(), $(fn), DOMContentLoaded and window.onload?
- How do you bind and unbind events in modern jQuery? What happened to bind, live and delegate?
- What is the difference between .attr() and .prop()?
- What is the difference between preventDefault(), stopPropagation() and returning false in a jQuery handler?
- What is jQuery, and is it still relevant today?