2

if I have 10 buttons, am I better to have an event listener that points to each button and calls a different function:

hdlButton1.addEventListener("click", fButton1Clicked);
hdlButton2.addEventListener("click", fButton2Clicked);
etc.

function fButton1Clicked(event) {
    //code to execute upon button 1 click
}
function fButton2Clicked(event) {
    //code to execute upon button 1 click
}

Or have all the clicks point to a single function:

 document.addEventListener("click", fButtonClick);

 function fButtonClick(event) {
     switch (event.target.id) {
         case "btn1":
              //code to handle button 1 click
              break;
          case "btn2":
              //code to handle button 2 click
              break;
        }
}

What are the implications for performance in having multiple functions to handle the various events versus having a single function that differentiates between the events and handles appropriately?

(Thanks)

6
  • 2
    Clicking takes time that is not comparable to the time needed to branch to the specific code, whichever of the two methods you use. This is a non-issue. Commented Mar 19, 2019 at 21:53
  • Both snippets do entirely different things. Comparing those makes little sense. Also the performance impact will probably be a few nanoseconds, nothing that matters. Commented Mar 19, 2019 at 21:55
  • What is easier - adding a new button and changing the logic of one function potentially introducing a bug for ten more buttons, or adding a new function that is not going to have that problem? Commented Mar 19, 2019 at 21:57
  • Why use an event listener when you can use an on click for this? Commented Mar 19, 2019 at 21:58
  • 1
    @SudoKid the onclick HTML attribute adds an event listener. If you meant he onlcick JS property on DOM nodes, then that's still adding an event listener. The only thing is that you cannot add more than one event listener via this route. Commented Mar 19, 2019 at 22:00

3 Answers 3

1

From a performance point of view, it is always recommended to have as few listeners as possible, especially if you dynamically add/remove buttons or elements which have listeners attached.

What you did in the second case is called event delegation, and it's really useful when you have dynamically created elements and you want the listener to be triggered on nodes created after the listener was added.

Performance difference will not be noticeable, unless you need it for a high-performance app (eg. a game), so you should usually go with whatever is more readable, or with delegation if you have dynamically changing elements.

I have actually created a small library to easier implement event delegation for created elements: https://github.com/Cristy94/dynamic-listener

Sign up to request clarification or add additional context in comments.

Comments

1

The performance impact is quite negligible on modern computers unless you have an unreasonably huge number of buttons and functions. Better to worry about code readability and maintainability first - only think about performance issues once you have evidence that it's a non-negligible problem.

Comments

0

The time difference is negligible in both the cases, however I think using different functions would have better performance as there is one less branch statement.

As far as the code readability goes, I use single function when the buttons are doing similar job with different parameters and different functions when the jobs are different.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.