From 6734d2324ec269b889f86d7853be4ccf803e0f34 Mon Sep 17 00:00:00 2001 From: Vitaliy Lagunov Date: Mon, 1 Aug 2016 08:08:47 +0300 Subject: [PATCH] Speed of selectors Selectors of the form tag#id.class are not fast selectors and can not be processed very quickly. They are working with O(n) so fix confusion in the documentation. --- entries/on.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entries/on.xml b/entries/on.xml index 18a17cae..1cf09e8d 100644 --- a/entries/on.xml +++ b/entries/on.xml @@ -88,7 +88,7 @@ $( "button" ).on( "click", {

Event performance

In most cases, an event such as click occurs infrequently and performance is not a significant concern. However, high frequency events such as mousemove or scroll can fire dozens of times per second, and in those cases it becomes more important to use events judiciously. Performance can be increased by reducing the amount of work done in the handler itself, caching information needed by the handler rather than recalculating it, or by rate-limiting the number of actual page updates using setTimeout.

Attaching many delegated event handlers near the top of the document tree can degrade performance. Each time the event occurs, jQuery must compare all selectors of all attached events of that type to every element in the path from the event target up to the top of the document. For best performance, attach delegated events at a document location as close as possible to the target elements. Avoid excessive use of document or document.body for delegated events on large documents.

-

jQuery can process simple selectors of the form tag#id.class very quickly when they are used to filter delegated events. So, "#myForm", "a.external", and "button" are all fast selectors. Delegated events that use more complex selectors, particularly hierarchical ones, can be several times slower--although they are still fast enough for most applications. Hierarchical selectors can often be avoided simply by attaching the handler to a more appropriate point in the document. For example, instead of $( "body" ).on( "click", "#commentForm .addNew", addComment ) use $( "#commentForm" ).on( "click", ".addNew", addComment ).

+

Delegated events that use complex selectors, particularly hierarchical ones, can be several times slower than selectors of the form tag#id.class. Hierarchical selectors can often be avoided simply by attaching the handler to a more appropriate point in the document. For example, instead of $( "body" ).on( "click", "#commentForm .addNew", addComment ) use $( "#commentForm" ).on( "click", ".addNew", addComment ).

Additional notes

There are shorthand methods for some events such as .click() that can be used to attach or trigger event handlers. For a complete list of shorthand methods, see the events category.

Deprecated in jQuery 1.8, removed in 1.9: The name "hover" used as a shorthand for the string "mouseenter mouseleave". It attaches a single event handler for those two events, and the handler must examine event.type to determine whether the event is mouseenter or mouseleave. Do not confuse the "hover" pseudo-event-name with the .hover() method, which accepts one or two functions.