EventEmitter 3 vs 4

JavaScript performance comparison

Test case created by Oliver Caldwell and last updated

Info

I am in the throws of rewriting my EventEmitter class. I just want to see how things are shaping up speed wise.

These tests are based on version 4 as it stood in this commit.

The versions of EventEmitter are slightly modified and stored in this gist. I have changed them so that they load under the EventEmitter3 and EventEmitter4 namespaces.

Preparation code

<script src="//gist.github.com/raw/3364020/fba327aa869714499ead3663b574598105940867/EventEmitter3.js"></script>

<script src="//gist.github.com/raw/3364020/837ef838f1d16c92e5cb0a8e98b2582675a27798/EventEmitter4.js"></script>

<script src="//ajax.googleapis.com/ajax/libs/mootools/1.3/mootools-yui-compressed.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var ee,
        fn1 = function(){},
        fn2 = function(){},
        fn3 = function(){},
        fn4 = function(){},
        fn5 = function(){},
        fn6 = function(){},
        fn7 = function(){};
};
</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
EventEmitter3 - Mixed
ee = new EventEmitter3();

ee.addListener('foo', fn1);
ee.addListener('foo', fn2);
ee.addListener('foo', fn3);
ee.addListener('foo', fn4);
ee.addListener('bar', fn5);
ee.addListener('bar', fn6);
ee.addListener('bar', fn7);

ee.emit('foo', 1, 2, 3);
ee.emit('bar', 4, 5);
ee.emit('foo', 6, 7);

ee.removeListener('foo', fn4);
ee.removeListener('bar', fn5);
ee.removeListener('bar', fn6);

ee.removeAllListeners('bar');
ee.removeAllListeners();
pending…
EventEmitter4 - Mixed
ee = new EventEmitter4();

ee.addListener('foo', fn1);
ee.addListener('foo', fn2);
ee.addListener('foo', fn3);
ee.addListener('foo', fn4);
ee.addListener('bar', fn5);
ee.addListener('bar', fn6);
ee.addListener('bar', fn7);


ee.emitEvent('foo', [1, 2, 3]);
ee.emitEvent('bar', [4, 5]);
ee.emitEvent('foo', [6, 7]);

ee.removeListener('foo', fn4);
ee.removeListener('bar', fn5);
ee.removeListener('bar', fn6);

ee.removeListeners('bar');
ee.removeListeners();
pending…
MooTools - Mixed
ee = new Events();

ee.addEvent('foo', fn1);
ee.addEvent('foo', fn2);
ee.addEvent('foo', fn3);
ee.addEvent('foo', fn4);
ee.addEvent('bar', fn5);
ee.addEvent('bar', fn6);
ee.addEvent('bar', fn7);

ee.fireEvent('foo', [1, 2, 3]);
ee.fireEvent('bar', [4, 5]);
ee.fireEvent('foo', [6, 7]);

ee.removeEvent('foo', fn4);
ee.removeEvent('bar', fn5);
ee.removeEvent('bar', fn6);

ee.removeEvents('bar');
ee.removeEvents();
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