Native Browser events vs jquery trigger

JavaScript performance comparison

Revision 5 of this test case created

Info

Comparison between three different methods of triggering events for use in unit testing.

  1. jQuery's trigger
  2. Native browser events using document.createEvent
  3. Native browser events using jQuery's simulate plugin

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/prototype/1/prototype.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="//johnkpaul.github.com/jquery-simulate/jquery.simulate.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    window.$el = jQuery("<div id='test'></div>").appendTo(document.body);
    window.el = window.$el.get(0);
};

Benchmark.prototype.teardown = function() {
    window.$el.remove();
};
</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
jQuery trigger
// async test
window.$el.one("click", function() {
  setTimeout(function() {
    deferred.resolve();
  }, 10);
});

window.$el.trigger("click");
pending…
native event
// async test
window.$el.one("click", function() {
  setTimeout(function() {
    deferred.resolve();
  }, 10);
})

var event = document.createEvent("MouseEvents");
event.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
window.el.dispatchEvent(event);
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