Created
August 22, 2011 19:58
-
-
Save bmeck/1163360 to your computer and use it in GitHub Desktop.
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
// A subclass of Socket which reads data by line | |
var net = require('net'); | |
var util = require('util'); | |
function Socket(options) { | |
if (!(this instanceof Socket)) return new Socket(options); | |
net.Socket.call(this, options); | |
this.current_data = []; | |
this.on('data', this.process_data); | |
this.on('end', this.process_end); | |
} | |
util.inherits(Socket, net.Socket); | |
exports.Socket = Socket; | |
var separator = '\n'.charCodeAt(0); | |
Socket.prototype.process_data = function process_data(data) { | |
if(typeof data === 'string') { | |
return process_data(new Buffer(data)); | |
} | |
var current_data = this.current_data; | |
var results; | |
var l = data.length; | |
var start = 0; | |
for(var i = 0; i < l;) { | |
if(data[i++] === separator) { | |
if(current_data.length) { | |
var this_line = current_data.join('') + data.slice(start, i); | |
current_data = this.current_data = []; | |
} | |
else { | |
var this_line = data.slice(start, i).toString(); | |
} | |
start = i; | |
this.emit('line', this_line); | |
} | |
} | |
if(start != l) { | |
current_data[current_data.length] = data; | |
} | |
}; | |
Socket.prototype.process_end = function () { | |
if (this.current_data.length) | |
this.emit('line', this.current_data.join('')) | |
this.current_data = []; | |
}; |
Author
bmeck
commented
Sep 1, 2011
via email
kk~ I hadn't benchmarked against the server, just with that for loop.
Do you use anything for profiling in node?
i know v8-profiler can be integrated with node-inspector for heap inspection
substack has https://github.com/substack/node-bunker
generally for small benchmarks I just use benchmark.js (
http://benchmarkjs.com/ )
…On Thu, Sep 1, 2011 at 3:21 PM, baudehlo ***@***.*** wrote:
Do you use anything for profiling in node?
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/1163360
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment