Created
January 23, 2012 15:44
-
-
Save AndreasMadsen/1663885 to your computer and use it in GitHub Desktop.
process.send blocks in node.js
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
$ node test | |
create: 678ms | |
send: 4445ms | |
200 ms passed, or not :/ | |
got message: 5 MB | |
get: 4472ms |
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 MB = 1024 * 1024; | |
if (process.argv[2] === 'child') { | |
// create a 5 MB template | |
console.time('create'); | |
var t = '', i = (MB * 5); | |
while(i--) { | |
t += 'a'; | |
} | |
console.timeEnd('create'); | |
setTimeout(function () { | |
console.log('200 ms passed, or not :/'); | |
}, 200); | |
// start timer | |
process.send('a'); | |
console.time('send'); | |
process.send(t); | |
console.timeEnd('send'); | |
} else { | |
var fork = require('child_process').fork; | |
var child = fork(process.argv[1], ['child']); | |
child.on('message', function (msg) { | |
var l = msg.length; | |
if (l === 1) { | |
console.time('get'); | |
} | |
else { | |
console.log('got message: ' + (msg.length / MB) + ' MB'); | |
console.timeEnd('get'); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment