Created
August 28, 2017 01:15
-
-
Save pokatomnik/4a9043197119693d1c937461bed511b3 to your computer and use it in GitHub Desktop.
This very simple example demonstrates why heavy CPU tasks is really bad idea for node.js (and browser javascript VM, of course)
This file contains hidden or 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
function load () { | |
for (var i=0; i<60000; ++i) { | |
for (var j=60000; j>=0; --j) { | |
a = i*j; | |
} | |
} | |
} | |
function cpuConsume(count, cpuLoadFunc) { | |
var num = 1; | |
return Promise | |
.all( | |
Array | |
.apply( | |
[], | |
new Array(count) | |
) | |
.map(function () { | |
return new Promise(function (resolve, reject) { | |
var a; | |
var big = big; | |
console.log('Thread ' + num++ + ' started'); | |
cpuLoadFunc(); | |
resolve(); | |
}); | |
}) | |
); | |
} | |
Promise | |
.resolve() | |
.then(function () { | |
console.time(oneThread); | |
return cpuConsume(1, load); | |
}) | |
.then(function () { | |
console.timeEnd(oneThread); | |
console.log('---------------------------------------'); | |
console.time(twoThreads); | |
return cpuConsume(2, load); | |
}) | |
.then(function () { | |
console.timeEnd(twoThreads); | |
}) | |
.catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment