Message passing: CustomEvents vs mitt (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script src="https://unpkg.com/mitt/dist/mitt.umd.js"></script>

Setup

// API web
/**
 * Trigger custom event
 * @param {string} eventName Custom event name
 * @param {any} data Data
 */
function triggerCustomEvent(eventName, data) {
    let event = null;
    if (window.CustomEvent && typeof window.CustomEvent === 'function') {
        event = new CustomEvent(eventName, { detail: data });
    } else {
        event = document.createEvent('CustomEvent');
        event.initCustomEvent(eventName, true, true, data);
    }
    document.body.dispatchEvent(event);
}

document.body.addEventListener(
	'body-foo',
	e => console.log(e.detail)
);

// MITT
const emitter = mitt()
emitter.on(
	'mitt-foo',
	e => console.log('foo', e)
);

Test runner

Ready to run.

Testing in
TestOps/sec
CustomEvent
// API web
triggerCustomEvent(
	'body-foo',
	{ a: 'b' }
);
ready
Mitt
emitter.emit(
	'mitt-foo',
	{ a: 'b' }
);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.