events vanilla and jquery

JavaScript performance comparison

Revision 13 of this test case created by Tomás Corral

Info

vanilla: http://codepen.io/CKGrafico/pen/Avuxl jquery: http://codepen.io/CKGrafico/pen/ALgdl

Preparation code

<style>
.show {
  display: block;
}

.hide {
  display: none;
}
</style>

<nav role="navigation">
  <ul class="nav">
   <li>
        <a href="#">Campaign</a>
        <ul class="hide">
          <li><a href="#">link01</a></li>
          <li><a href="#">link02</a></li>
          <li><a href="#">link03</a></li>
          <li><a href="#">link04</a></li>
        </ul>
      </li>
   <li><a href="">History</a></li>
   <li><a href="">Shop</a></li>
   <li><a href="">Social</a></li>
  </ul>
 </nav><script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></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
vanilla click
(function() {

  var test = document.querySelectorAll("nav ul li");
  [].forEach.call(test, function(e) {
    e.addEventListener('click', toggle);
  });

  function toggle(e) {
    e.preventDefault();
    var target = e.currentTarget;
    [].forEach.call(target.childNodes, function(node) {
      if (node.className == 'hide') {
        node.className = 'show';
      } else if (node.className == 'show') {
        node.className = 'hide';
      }
    });
  }
})();
pending…
jquery
(function($) {
  $('nav ul li').on('click', toggle);

  function toggle(e) {
    e.preventDefault();
    var $listChild = $(this).find('ul');
    if ($listChild.hasClass('hide') || $listChild.hasClass('show')) {
      $listChild.toggleClass('hide');
      $listChild.toggleClass('show');
    }
  }

})(jQuery);
pending…
Vanilla click (delegation)
(function() {

  var test = document.querySelectorAll("nav");
  [].forEach.call(test, function(e) {
    e.addEventListener('click', toggle);
  });

  function toggle(e) {
    e.preventDefault();
    var target = e.target;
    if (target && target.nodeName == "A") {
      target = target.parentNode;
    }
    [].forEach.call(target.childNodes, function(node) {
      if (node.className == 'hide') {
        node.className = 'show';
      } else if (node.className == 'show') {
        node.className = 'hide';
      }
    });
  }
})();
pending…
Vanilla without forEach
(function() {
  var aElements = document.querySelectorAll("nav"),
    oElement;
  while ( !! (oElement = [].shift.call(aElements))) {
    oElement.addEventListener('click', toggle);
  }

  function toggle(eEvent) {
    var target = eEvent.target,
      aChildNodes,
      node;
    if (target && target.nodeName == "A") {
      target = target.parentNode;
    }
    eEvent.preventDefault();
    aChildNodes = target.childNodes;
    while ( !! (node = [].shift.call(aChildNodes))) {
      if (node.className == 'hide') {
        node.className = 'show';
      } else if (node.className == 'show') {
        node.className = 'hide';
      }
    }
  }
})();
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