Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
javascript one eventlistener to rule them all vs multiple listeners (best practice)

Hi I have been looking on the internet trying to figure out the best practice for adding eventlistener to my websites and it generally comes down with two approaches as follow:

Adding an event listener to every elements (e.g. button) using a loop:

document.querySelectorAll('.some-button').forEach(item => {
    item.addEventListener('click', event => {
        //handle click
    })
})

Or using event bubbling to use one listener only and check the e.target for each scenario:

body.addEventListener('click', event => {
    if (event.target !== element1 && event.target !== element2) {
        //return
    }
    //handle click
}

However, I was unable to find any article comparing the performance of these 2 approaches. From my limited understanding, whenever there is a 'click' javascript will loop through all the eventlisteners anyway (even though if you didn't click on it) and for that reason I am taking the second approach (using one eventlistener) which seems to be the easier one to manage.

Appreciate if someone can provide further insights on the potential drawback of the second approach and if there will be any performance issue thanks.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!