1

I have already checked some posts like event-listener-vs-event-handler and onclick-vs-event-handler comparing the two.. but not in the performance perspective.

Of course using unobtrusive js is best practice but when it comes to performance?

Imagine we have 100 elements to listen to; which one will perform faster and make our app light and not consume lot of memory?

<button onclick="myFunction()">Click me</button>

or the event listener like in jQuery for example:

$( "#target" ).click(function() {
  alert( "Handler for .click() called." );
});

UPDATE

Lets imagine worst scenarios; we want to run our web app in cheap mobile phone with 256 MB of ram! Having to listen to many events will take some considerable space in the memory (I guess!) making the browser "slow" to respond.. but using the onclick option doesn't require the browser to cash all this events, but, fire them when needed (clicked). Would be great to see some test reports.

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
numediaweb
  • 16,362
  • 12
  • 74
  • 110
  • jQuery has far worse performance than the native feature. But I'm not saying that you should use an inline listener. – Bergi Sep 02 '14 at 19:59
  • 1
    ... but it doesn't matter because modern browsers can handler the processing involved with a single event extremely quickly. This is definitely a case where the overriding consideration should be maintainability and clarity. – Pointy Sep 02 '14 at 19:59
  • 7
    Microoptimization, useless. I'd worry about maintainability. – elclanrs Sep 02 '14 at 20:00
  • I really can't even imagine a scenario where doing this inline would be better. Good question though! – Jorge Silva Sep 02 '14 at 20:01
  • 1
    And if you have 100 elements to listen to, I'd consider event delegation, before even considering inline events. – elclanrs Sep 02 '14 at 20:02
  • 2
    It is a micro-optimization unless (1) you're doing performance testing, and (2) 100 elements with events is a possible operation scenario, and (3) the application is failing this performance test. – kevin628 Sep 02 '14 at 20:03
  • a simple delegated event should attach faster than either direct bind option. – dandavis Sep 02 '14 at 20:05

0 Answers0