Skip to content
Closed
Changes from 1 commit
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
46 changes: 46 additions & 0 deletions benchmark/worker/messageport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

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

function main(conf) {

const n = +conf.n;
Comment thread
addaleax marked this conversation as resolved.
Outdated
const sends = +conf.sendsPerBroadcast;

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);
}
}