events vanilla and jquery

JavaScript performance comparison

Revision 18 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
// async test
(function() {

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

  function toggle(e) {
    e.preventDefault();
    this.removeEventListener('click', toggle, false);
    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';
      }
      deferred.resolve()
    });
  }
  document.querySelector('li').click();
})();
pending…
jquery
// async test
(function($) {
  var test = $('nav ul li').on('click', toggle);

  function toggle(e) {
    e.preventDefault();
    $(this).off('click', toggle);
    var $listChild = $(this).find('ul');
    if ($listChild.hasClass('hide') || $listChild.hasClass('show')) {
      $listChild.toggleClass('hide');
      $listChild.toggleClass('show');
      deferred.resolve()
    }
  }
  document.querySelector('li').click();
})(jQuery);
pending…
Vanilla click (delegation)
// async test
(function() {

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

  function toggle(e) {
    e.preventDefault();
    this.removeEventListener('click', toggle, false);
    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';
      }
      deferred.resolve()
    });
  }
  document.querySelector('li').click();
})();
pending…
vanilla click (v2)
// async test
(function() {

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

  function toggle(e) {
    e.preventDefault();
    this.removeEventListener('click', toggle, false);
    var target = e.currentTarget;
    Array.prototype.slice.call(target.childNodes).forEach(function(node) {
      if (node.className == 'hide') {
        node.className = 'show';
      } else if (node.className == 'show') {
        node.className = 'hide';
      }
      deferred.resolve()
    });
  }
  document.querySelector('li').click();
})();
pending…
vanilla click (v3)
// async test
(function() {

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

  function toggle(e) {
    e.preventDefault();
    this.removeEventListener('click', toggle, false);
    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';
      }
      deferred.resolve()
    });
  }
  document.querySelector('li').click();
})();
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