Combined or Separate Event Handlers

JavaScript performance comparison

Revision 4 of this test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="elm"></div>
<script>
Benchmark.prototype.setup = function() {
    var $doc = $(document), $elm = $('#elm');
    var clickFunc = function(e) { console.log('click'); };
    var keyDownFunc = function(e) { console.log('keydown'); };
    var keyUpFunc = function(e) { console.log('keyup'); };
    var focusFunc = function(e) { console.log('focus'); };
    var blurFunc = function(e) { console.log('blur'); };
    var mouseOverFunc = function(e) { console.log('mouseover'); };
    var mouseOutFunc = function(e) { console.log('mouseout'); };
};

Benchmark.prototype.teardown = function() {
    $doc.off('click keydown keyup focus blur mouseover mouseout');
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Combined
$doc.on('click keydown keyup focus blur mouseover mouseout', '#elm', function(e){
  switch(e.type) {
    case 'click':
      clickFunc();
      break;
    case 'keydown':
      keyDownFunc();
      break;
    case 'keyup':
      keyUpFunc();
      break;
    case 'focus':
      focusFunc();
      break;
    case 'blur':
      blurFunc();
      break;
    case 'mouseover':
      mouseOverFunc();
      break;
    case 'mouseout':
      mouseOutFunc();
      break;
  }
});

$elm.trigger('click');
$elm.trigger('keydown');
$elm.trigger('keyup');
$elm.trigger('focus');
$elm.trigger('blur');
$elm.trigger('mouseover');
$elm.trigger('mouseout');
pending…
Separate
$doc.on('click', '#elm', clickFunc);

$doc.on('keydown', '#elm', keyDownFunc);

$doc.on('keyup', '#elm', keyUpFunc);

$doc.on('focus', '#elm', focusFunc);

$doc.on('blur', '#elm', blurFunc);

$doc.on('mouseover', '#elm', mouseOverFunc);

$doc.on('mouseout', '#elm', mouseOutFunc);

$elm.trigger('click');
$elm.trigger('keydown');
$elm.trigger('keyup');
$elm.trigger('focus');
$elm.trigger('blur');
$elm.trigger('mouseover');
$elm.trigger('mouseout');
pending…
if/else
$doc.on('click keydown keyup focus blur mouseover mouseout', '#elm', function(e){
  var type = e.type;
  if (type === 'click') {
    clickFunc();
  } else if (type === 'keydown') {
    keyDownFunc();
  } else if (type === 'keyup') {
    keyUpFunc();
  } else if (type === 'focus') {
    focusFunc();
  } else if (type === 'blur') {
    blurFunc();
  } else if (type === 'mouseover') {
    mouseOverFunc();
  } else if (type === 'mouseout') {
    mouseOutFunc();
  }
});

$elm.trigger('click');
$elm.trigger('keydown');
$elm.trigger('keyup');
$elm.trigger('focus');
$elm.trigger('blur');
$elm.trigger('mouseover');
$elm.trigger('mouseout');
pending…
object handler
$doc.on({
        'click': clickFunc,
        'keydown': keyDownFunc,
        'keyup': keyUpFunc,
        'focus': focusFunc,
        'blur': blurFunc,
        'mouseover': mouseOverFunc,
        'mouseout': mouseOutFunc
}, '#elm');

$elm.trigger('click');
$elm.trigger('keydown');
$elm.trigger('keyup');
$elm.trigger('focus');
$elm.trigger('blur');
$elm.trigger('mouseover');
$elm.trigger('mouseout');
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment