Skip to content

Instantly share code, notes, and snippets.

@jthomas
Created May 10, 2019 15:08
Show Gist options
  • Save jthomas/23b6f89e8f205346e03e339864af1851 to your computer and use it in GitHub Desktop.
Save jthomas/23b6f89e8f205346e03e339864af1851 to your computer and use it in GitHub Desktop.
workers.js
'use strict';
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
const min = 2
function generatePrimes(start, range) {
const primes = []
let isPrime = true;
let end = start + range;
for (let i = start; i < end; i++) {
for (let j = min; j < Math.sqrt(end); j++) {
if (i !== j && i%j === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes.push(i);
}
isPrime = true;
}
return primes
}
const primes = generatePrimes(workerData.start, workerData.range);
parentPort.postMessage(primes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment