Cross browser event

JavaScript performance comparison

Revision 2 of this test case created by Diggo11

Preparation code

<script type="text/javascript">
  var pseudoEvent = document.createEvent('MouseEvents');
  pseudoEvent.initEvent('click', true, true);
</script>
<div id="testDiv">
  &nbsp;
</div>
<script>
Benchmark.prototype.setup = function() {
    function testTypeof(e) {
      if (typeof e == 'undefined') {
        e = window.event;
      }
      if (typeof e.target != 'undefined') {
        var output = e.target;
      } else {
        var output = e.srcElement;
      }
    }
   
    function testBooleanOutput(e) {
      if (typeof e == 'undefined') {
        e = window.event;
      }
      var output = e.target || e.srcElement;
    }
   
    function testInline(e) {
      if (typeof e == 'undefined') {
        e = window.event;
      }
      var output = e.target ? e.target : e.srcElement;
    }
   
    function testBooleanIO(e) {
      if (!e) {
        e = window.event;
      }
      var output = e.target || e.srcElement;
    }
   
    function testSwitch(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
var input = document.getElementById('testDiv');
input.addEventListener('click', testTypeof, false);
input.dispatchEvent(pseudoEvent);
input.removeEventListener('click', testTypeof, false);
pending…
testBooleanOutput
var input = document.getElementById('testDiv');
input.addEventListener('click', testBooleanOutput, false);
input.dispatchEvent(pseudoEvent);
input.removeEventListener('click', testBooleanOutput, false);
pending…
testInline
var input = document.getElementById('testDiv');
input.addEventListener('click', testInline, false);
input.dispatchEvent(pseudoEvent);
input.removeEventListener('click', testInline, false);
pending…
testBooleanIO
var input = document.getElementById('testDiv');
input.addEventListener('click', testBooleanIO, false);
input.dispatchEvent(pseudoEvent);
input.removeEventListener('click', testBooleanIO, false);
pending…
testSwitch
var input = document.getElementById('testDiv');
input.addEventListener('click', testSwitch, false);
input.dispatchEvent(pseudoEvent);
input.removeEventListener('click', testSwitch, false);
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