Your code will encounter this error, you can see it while listening to the error on the reader and writer:
[Error: EMFILE, open 'out.txt'] errno: 20, code: 'EMFILE', path: 'in.txt' [Error: EMFILE, open 'out.txt'] errno: 20, code: 'EMFILE', path: 'out.txt'
This is because threads are asynchronous and without an explicit callback when they end. This way, you pretty much create thousands and thousands of stream flows between in.txt and out.txt until the system tells you that too many file descriptors are open.
So, I assume that by “comparing threads in Node”, what you want to compute is the time it takes to execute this operation synchronously:
reader.pipe(filter).pipe(writer)
In this case, you will need:
The code for this answer has been tested with node 0.10.0, but I believe that the only difference should be in the name of the module that contains the Transform :
var fs = require('fs'); var util = require('util'); var Transform = require('stream').Transform; var Benchmark = require('benchmark'); var suite = new Benchmark.Suite; var i = 0; // my super uppercase stream function Uppercase(options) { if (!(this instanceof Uppercase)) return new Uppercase(options); Transform.call(this, options); } Uppercase.prototype = Object.create( Transform.prototype, { constructor: { value: Uppercase }} ); Uppercase.prototype._transform = function(chunk, encoding, done) { chunk = chunk.toString().toUpperCase(); this.push(chunk) }; // start benchmarking suite.add('stream test', { 'defer' : true, 'fn' : function (deferred) { var reader = fs.createReadStream('in.txt'); var parser = new Uppercase(); var writer = fs.createWriteStream('out.txt'); reader.on('error', function (err) { console.log(err); }); writer.on('error', function (err) { console.log(err); }); reader.on('end', function (argument) { // Wait until reader is over and then close reader and finish deferred test writer.end(); deferred.resolve(); }); reader.pipe(parser).pipe(writer, {'end': false}); } }) // listeners .on('cycle', function(event) { console.log(String(event.target)); }) .on('complete', function() { console.log('Fastest is ' + this.filter('fastest').pluck('name')); }) // run async .run();