events vanilla and jquery

JavaScript performance comparison

Revision 2 of this test case created

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) {
        console.log(e);
        e.preventDefault();
        var target = e.currentTarget;
    debugger
        [].forEach.call(target.getElementsByTagName('ul'), 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…

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