Cross browser event

JavaScript performance comparison

Revision 8 of this test case created

Preparation code

<script type="text/javascript">
window.makeEvt = function() {
  var pseudoEvent = document.createEvent('MouseEvents');
  pseudoEvent.initMouseEvent("click", true, true, window,
            0, 0, 0, 0, 0, false, false, false, false, 0, null);  
  return pseudoEvent;
}
</script>
<div id="testDiv">
  &nbsp;
</div>
<script>
Benchmark.prototype.setup = function() {
    window.input = document.getElementById('testDiv');
    window.addHandlers = [
        function() { input.addEventListener('click', testTypeof, false); },
        function() { input.addEventListener('click', testBooleanOutput, false); },
        function() { input.addEventListener('click', testInline, false); },
        function() { input.addEventListener('click', testSwitch, false); }
    ];
    window.removeHandlers = [
        function() { input.removeEventListener('click', testTypeof, false); },
        function() { input.removeEventListener('click', testBooleanOutput, false); },
        function() { input.removeEventListener('click', testInline, false); },
        function() { input.removeEventListener('click', testSwitch, false); }
    ];
   
    for (var i = 0; i < 4; i++) {
        ui.benchmarks[i].setup = window.addHandlers[i];
        ui.benchmarks[i].teardown = window.removeHandlers[i];
    }
   
    window.testTypeof = function(e) {
      if (typeof e == 'undefined') {
        e = window.event;
      }
      if (typeof e.target != 'undefined') {
        var output = e.target;
      } else {
        var output = e.srcElement;
      }
    }
   
    window.testBooleanOutput = function(e) {
      if (typeof e == 'undefined') {
        e = window.event;
      }
      var output = e.target || e.srcElement;
    }
   
    window.testInline = function(e) {
      if (typeof e == 'undefined') {
        e = window.event;
      }
      var output = e.target ? e.target : e.srcElement;
    }
   
    window.testBooleanIO = function(e) {
      if (!e) {
        e = window.event;
      }
      var output = e.target || e.srcElement;
    }
   
    window.testSwitch = function(e) {
      var output;
      switch (typeof e) {
      case 'object':
        output = e.target;
        break;
      case 'undefined':
        output = window.event.srcElement;
        break;
      }
    }
};
</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
testTypeof
input.dispatchEvent(makeEvt());
pending…
testBooleanOutput
input.dispatchEvent(makeEvt());
pending…
testInline
input.dispatchEvent(makeEvt());
pending…
testBooleanIO
input.dispatchEvent(makeEvt());
pending…
testSwitch
input.dispatchEvent(makeEvt());
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