events vanilla and jquery

JavaScript performance comparison

Revision 10 of this test case created by Jorge del Casar

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 navs = document.querySelectorAll("nav");
  for (var i = navs.length - 1; i >= 0; i--) {
    navs[i].addEventListener('click', toggle, true);
  }

  function toggle(e) {
    e.preventDefault();
    var target = e.target;
    if (target && target.nodeName == "A") {
      target = target.parentNode;
    }
    if (target && target.nodeName == "LI") {
      var sublists = target.querySelectorAll('ul');
      for (var i = sublists.length - 1; i >= 0; i--) {
        sublists[i].classList.toggle('hide')
        sublists[i].classList.toggle('show')
      }
    }
  }
})();
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…

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