Created
July 9, 2018 09:18
-
-
Save noopnik/a4636a98040ae65140a1704afced7cc9 to your computer and use it in GitHub Desktop.
zemelya
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Worker = require('webworker-threads').Worker; | |
var promises = []; | |
var start = new Date(); | |
for (let worker = 0; worker < 4; worker++) { | |
var promise = new Promise((resolve, reject) => { | |
setTimeout(() => { | |
var fibo = new Worker(function() { | |
function fibonacci(num) { | |
if (num <= 1) return 1; | |
return fibonacci(num - 1) + fibonacci(num - 2); | |
} | |
this.onmessage = function (event) { | |
var result = fibonacci(event.data) | |
postMessage(result); | |
}; | |
}); | |
fibo.onmessage = function (event) { | |
var end = new Date(); | |
var seconds = (end.getTime() - start.getTime()) / 1000; | |
console.log(seconds) | |
console.log(new Date()); | |
resolve(); | |
}; | |
fibo.postMessage(45); | |
}, 0); | |
// setTimeout(() => { | |
// function fibonacci(num) { | |
// if (num <= 1) return 1; | |
// | |
// return fibonacci(num - 1) + fibonacci(num - 2); | |
// } | |
// | |
// const start = new Date(); | |
// console.log('worker: ', worker, start); | |
// fibonacci(45); | |
// const end = new Date(); | |
// var seconds = (end.getTime() - start.getTime()) / 1000; | |
// console.log('worker: ', worker, seconds); | |
// resolve(); | |
// }, 0); | |
}); | |
promises.push(promise); | |
} | |
console.log('MAJOR START: ', start); | |
Promise.all(promises).then(() => { | |
var end = new Date(); | |
var seconds = (end.getTime() - start.getTime()) / 1000; | |
console.log('MAJOR END: ', seconds); | |
console.log(new Date()); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment