Native Browser events vs jquery trigger

JavaScript performance comparison

Test case created by John K Paul and last updated

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/jquery/1/jquery.min.js">
</script>
<script src="//johnkpaul.github.com/jquery-simulate/jquery.simulate.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var $el = jQuery("<div id='test'></div>").appendTo("body");
    var el = $el.get(0);
   
    var events = [];
    $el.on("click", function(e) {
      events.push(e);
    });
};

Benchmark.prototype.teardown = function() {
    $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
$el.trigger("click");
pending…
jQuery triggerHandlers
$el.triggerHandler("click");
pending…
native event
var mouseEvent = document.createEvent("MouseEvents");
mouseEvent.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
el.dispatchEvent(mouseEvent);
pending…
native event through simulate plugin
$el.simulate("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