Skip to content

Instantly share code, notes, and snippets.

@steevehook
Forked from deleteman/worker-thread-code.js
Created June 29, 2018 08:10
Show Gist options
  • Save steevehook/4bfe4423a36e78669f5a41aba744d397 to your computer and use it in GitHub Desktop.
Save steevehook/4bfe4423a36e78669f5a41aba744d397 to your computer and use it in GitHub Desktop.
const { parentPort } = require('worker_threads');
function random(min, max) {
return Math.random() * (max - min) + min
}
const sorter = require("./test2-worker");
const start = Date.now()
let bigList = Array(1000000).fill().map( (_) => random(1,10000))
/**
//you can receive messages from the main thread this way:
parentPort.on('message', (msg) => {
console.log("Main thread finished on: ", (msg.timeDiff / 1000), " seconds...");
})
*/
sorter.sort(bigList);
parentPort.postMessage({ val: sorter.firstValue, timeDiff: Date.now() - start});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment