Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Are there performance issues depending on which elements triggers a jquery event?

 $('body').on('myEvent',function(e){
    // access to e properties
 });


 $('#innerDiv').on('myEvent',function(e){
   // access to e properties
 });

Which one is better? In the first case "e" carries more information than "e" returned in the second snippet. So the "e" returned in the first case is a bigger object than the "e" returned in the second case. The question is, does this affects javascript performances? Heap? Memory? Or this object "e", indipendently from his size is already cached and easy-accessible?

Is it a good practice make "lighter objects" triggering events instead the entire body, document or html?

share|improve this question
    
as ID must be unique on context page, the second one would be the best option here –  A. Wolff Nov 15 '13 at 10:06

1 Answer 1

Event Delegation will be the better option

$('body').on('myEvent', '#innerDiv' ,function(e){
     // access to e properties
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.