Should all jquery events be bound to $(document)?

后端 未结 4 1060
情深已故
情深已故 2020-11-21 07:48

Where this is coming from

When I first learned jQuery, I normally attached events like this:

$(\'.my-widget a\').click(function() {
    $(this).tog         


        
相关标签:
4条回答
  • 2020-11-21 08:23

    I'd like to add some remarks and counterarguments to jfriend00's answer. (mostly just my opinions based on my gut feeling)

    No - you should NOT bind all delegated event handlers to the document object. That is probably the worst performing scenario you could create.

    First off, event delegation does not always make your code faster. In some cases, it's is advantageous and in some cases not. You should use event delegation when you actually need event delegation and when you benefit from it. Otherwise, you should bind event handlers directly to the objects where the event happens as this will generally be more efficient.

    While it's true that performance might be slightly better if you are only going to register and event for a single element, I believe it doesn't weigh up against the scalability benefits that delegation brings. I also believe browsers are (going to be) handling this more and more efficiently, although I have no proof of this. In my opinion, event delegation is the way to go!

    Second off, you should NOT bind all delegated events at the document level. This is exactly why .live() was deprecated because this is very inefficient when you have lots of events bound this way. For delegated event handling it is MUCH more efficient to bind them to the closest parent that is not dynamic.

    I kind of agree on this. If you are 100% sure that an event will only happen inside a container, it makes sense to bind the event to this container, but I would still argue against binding events to the triggering element directly.

    Third off, not all events work or all problems can be solved with delegation. For example, if you want to intercept key events on an input control and block invalid keys from being entered into the input control, you cannot do that with delegated event handling because by the time the event bubbles up to the delegated handler, it has already been processed by the input control and it's too late to influence that behavior.

    This is simply not true. Please see this codePen: https://codepen.io/pwkip/pen/jObGmjq

    document.addEventListener('keypress', (e) => {
        e.preventDefault();
    });
    

    It illustrates how you can prevent a user from typing by registering the keypress event on the document.

    Here are times when event delegation is required or advantageous:

    When the objects you are capturing events on are dynamically created/removed and you still want to capture events on them without having to explicitly rebind event handlers every time you create a new one. When you have lots of objects that all want the exact same event handler (where lots is at least hundreds). In this case, it may be more efficient at setup time to bind one delegated event handler rather than hundreds or more direct event handlers. Note, delegated event handling is always less efficient at run-time than direct event handlers.

    I'd like to reply with this quote from https://ehsangazar.com/optimizing-javascript-event-listeners-for-performance-e28406ad406c

    Event delegation promotes binding as few DOM event handlers as possible, since each event handler requires memory. For example, let’s say that we have an HTML unordered list we want to bind event handlers to. Instead of binding a click event handler for each list item (which may be hundreds for all we know), we bind one click handler to the parent unordered list itself.

    Also, googling for performance cost of event delegation google returns more results in favor of event delegation.

    When you're trying to capture (at a higher level in your document) events that occur on any element in the document. When your design is explicitly using event bubbling and stopPropagation() to solve some problem or feature in your page. To understand this a little more, one needs to understand how jQuery delegated event handlers work. When you call something like this:

    $("#myParent").on('click', 'button.actionButton', myFn); It installs a generic jQuery event handler on the #myParent object. When a click event bubbles up to this delegated event handler, jQuery has to go through the list of delegated event handlers attached to this object and see if the originating element for the event matches any of the selectors in the delegated event handlers.

    Because selectors can be fairly involved, this means that jQuery has to parse each selector and then compare it to the characteristics of the original event target to see if it matches each selector. This is not a cheap operation. It's no big deal if there is only one of them, but if you put all your selectors on the document object and there were hundreds of selectors to compare to every single bubbled event, this can seriously start to hobble event handling performance.

    For this reason, you want to set up your delegated event handlers so a delegated event handler is as close to the target object as practical. This means that fewer events will bubble through each delegated event handler, thus improving the performance. Putting all delegated events on the document object is the worst possible performance because all bubbled events have to go through all delegated event handlers and get evaluated against all possible delegated event selectors. This is exactly why .live() is deprecated because this is what .live() did and it proved to be very inefficient.

    Where is this documented? If that's true, then jQuery seems to be handling delegation in a very inefficient way, and then my counter-arguments should only be applied to vanilla JS.

    Still: I would like to find an official source supporting this claim.

    :: EDIT ::

    Seems like jQuery is indeed doing event bubbling in a very inefficient way (because they support IE8) https://api.jquery.com/on/#event-performance

    So most of my arguments here only hold true for vanilla JS and modern browsers.

    0 讨论(0)
  • 2020-11-21 08:28

    Apparently, event delegation is actually recommended now. at least for vanilla js.

    https://gomakethings.com/why-event-delegation-is-a-better-way-to-listen-for-events-in-vanilla-js/

    "Web performance # It feels like listening to every click in the document would be bad for performance, but it’s actually more performant than having a bunch of event listeners on individual items."

    0 讨论(0)
  • 2020-11-21 08:29

    No - you should NOT bind all delegated event handlers to the document object. That is probably the worst performing scenario you could create.

    First off, event delegation does not always make your code faster. In some cases, it's is advantageous and in some cases not. You should use event delegation when you actually need event delegation and when you benefit from it. Otherwise, you should bind event handlers directly to the objects where the event happens as this will generally be more efficient.

    Second off, you should NOT bind all delegated events at the document level. This is exactly why .live() was deprecated because this is very inefficient when you have lots of events bound this way. For delegated event handling it is MUCH more efficient to bind them to the closest parent that is not dynamic.

    Third off, not all events work or all problems can be solved with delegation. For example, if you want to intercept key events on an input control and block invalid keys from being entered into the input control, you cannot do that with delegated event handling because by the time the event bubbles up to the delegated handler, it has already been processed by the input control and it's too late to influence that behavior.

    Here are times when event delegation is required or advantageous:

    • When the objects you are capturing events on are dynamically created/removed and you still want to capture events on them without having to explicitly rebind event handlers every time you create a new one.
    • When you have lots of objects that all want the exact same event handler (where lots is at least hundreds). In this case, it may be more efficient at setup time to bind one delegated event handler rather than hundreds or more direct event handlers. Note, delegated event handling is always less efficient at run-time than direct event handlers.
    • When you're trying to capture (at a higher level in your document) events that occur on any element in the document.
    • When your design is explicitly using event bubbling and stopPropagation() to solve some problem or feature in your page.

    To understand this a little more, one needs to understand how jQuery delegated event handlers work. When you call something like this:

    $("#myParent").on('click', 'button.actionButton', myFn);
    

    It installs a generic jQuery event handler on the #myParent object. When a click event bubbles up to this delegated event handler, jQuery has to go through the list of delegated event handlers attached to this object and see if the originating element for the event matches any of the selectors in the delegated event handlers.

    Because selectors can be fairly involved, this means that jQuery has to parse each selector and then compare it to the characteristics of the original event target to see if it matches each selector. This is not a cheap operation. It's no big deal if there is only one of them, but if you put all your selectors on the document object and there were hundreds of selectors to compare to every single bubbled event, this can seriously start to hobble event handling performance.

    For this reason, you want to set up your delegated event handlers so a delegated event handler is as close to the target object as practical. This means that fewer events will bubble through each delegated event handler, thus improving the performance. Putting all delegated events on the document object is the worst possible performance because all bubbled events have to go through all delegated event handlers and get evaluated against all possible delegated event selectors. This is exactly why .live() is deprecated because this is what .live() did and it proved to be very inefficient.


    So, to achieve optimized performance:

    1. Only use delegated event handling when it actually provides a feature you need or increases performance. Don't just always use it because it's easy because when you don't actually need it. It actually performs worse at event dispatch time than direct event binding.
    2. Attach delegated event handlers to the nearest parent to the source of the event as possible. If you are using delegated event handling because you have dynamic elements that you want to capture events for, then select the closest parent that is not itself dynamic.
    3. Use easy-to-evaluate selectors for delegated event handlers. If you followed how delegated event handling works, you will understand that a delegated event handler has to be compared to lots of objects lots of times so picking as efficient a selector as possible or adding simple classes to your objects so simpler selectors can be used will increase the performance of delegated event handling.
    0 讨论(0)
  • 2020-11-21 08:39

    Event delegation is a technique to write your handlers before the element actually exist in DOM. This method has its own disadvantages and should be used only if you have such requirements.

    When should you use event delegation?

    1. When you bind a common handler for more elements that needs same functionality. (Ex: table row hover)
      • In the example, if you had to bind all rows using direct bind, you would end up creating n handler for n rows in that table. By using delegation method you could end up handling all those in 1 simple handler.
    2. When you add dynamic contents more frequently in DOM (Ex: Add/remove rows from a table)

    Why you should not use event delegation?

    1. Event delegation is slower when compared to binding the event directly to element.
      • It compares the target selector on every bubble it hits, the comparison will be as expensive as it is complicated.
    2. No control over the event bubbling until it hits the element that it is bound to.

    PS: Even for dynamic contents you don't have to use event delegation method if you are bind the handler after the contents get inserted into DOM. (If the dynamic content be added not frequently removed/re-added)

    0 讨论(0)
提交回复
热议问题