BroadcastChannel Performance

Benchmark created on


Description

I was considering if I could use the BroadcastChannel API from inside Workers to get logs back to the main thread UI, since that seemed simpler than wiring up message passing through all classes. So I wanted to see how much worse they fare with lots of small messages.

Setup

// create worker with inline script blob
let script = "let bc = new BroadcastChannel('broadcast'); onmessage = ev => { for (let i = 0; i <= ev.data.n; i++) { if (ev.data.broadcast) { bc.postMessage({ i }) } else { postMessage({ i }) }; }; }";
let blob = URL.createObjectURL(new Blob([script]));
let worker = new Worker(blob);
console.log(blob);

// pick a common n for the loops
let n = 10_000;

Teardown

worker.terminate();

Test runner

Ready to run.

Testing in
TestOps/sec
Worker.postMessage
worker.onmessage = ({ data }) => {
	if (data.i == n) deferred.resolve();
}
worker.postMessage({ n, broadcast: false });
ready
BroadcastChannel.postMessage
let bc = new BroadcastChannel("broadcast");
bc.onmessage = ({ data }) => {
	if (data.i == n) {
		bc = bc.onmessage = null;
		deferred.resolve();
	}
}
worker.postMessage({ n, broadcast: true });
ready

Revisions

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