Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does performance suffer if I attach .on('click' events to $("body")?

I currently have the following code:

$('#loginLink, #registerLink').click(function () {
        dialog(this);
        return false;
    });

$('#detailData')
    .on('click', '.modalDeleteLink, .modalEditLink', function () {
        dialog(this);
        return false;
    })

There are just one of #loginLink and #registerLink but there could be up to a hundred elements with the classes .modalDeleteLink and .modalEditLink.

I was thinking to change all of these elements so they have a class of .dialogLink and then just using the following code:

$("body").on("click", ".dialogLink", function(){
   dialog(this);
   return false;
});

This code is a lot easier to maintain. However would there be any performance disadvantage in attaching this to the body?

like image 623
Angela Avatar asked Sep 03 '12 16:09

Angela


People also ask

What causes performance anxiety?

Simply put, stress and anxiety about performing in front of people causes performance anxiety. Confronting your fears and vulnerabilities, accepting yourself for who you are, and not feeling like you have to prove yourself to others, is the first step toward overcoming performance anxiety.

What are the negative effects of stress on the body?

Stress that's left unchecked can contribute to many health problems, such as high blood pressure, heart disease, obesity and diabetes.

How does state anxiety affect performance?

It causes an increase in hear rate and blood pressure, but also increased nervousness. Anxiety stimulates our “fight or flight” response, releasing adrenaline into the blood stream, and usually has a negative affect on performance. Both trait and state anxiety need to be controlled in the sporting context.


1 Answers

You don't need to worry about performance, unless you have hundreds of handlers or extremely large document.

Extract from documentation:

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.

like image 104
Michal Klouda Avatar answered Sep 30 '22 17:09

Michal Klouda