Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark: add MessagePort benchmark #31568

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions benchmark/worker/messageport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

const common = require('../common.js');
const { MessageChannel } = require('worker_threads');
const bench = common.createBenchmark(main, {
lundibundi marked this conversation as resolved.
Show resolved Hide resolved
payload: ['string', 'object'],
n: [1e6]
});

function main(conf) {

const n = +conf.n;
addaleax marked this conversation as resolved.
Show resolved Hide resolved

let payload;

switch (conf.payload) {
case 'string':
payload = 'hello world!';
break;
case 'object':
payload = { action: 'pewpewpew', powerLevel: 9001 };
break;
default:
throw new Error('Unsupported payload type');
}

const { port1, port2 } = new MessageChannel();

let messages = 0;
port2.onmessage = () => {
if (messages++ === n) {
bench.end(n);
port1.close();
} else {
write();
}
};
bench.start();
write();

function write() {
port1.postMessage(payload);
}
}