Try an array join instead of string concatenation to avoid so many unnecessary intermediate versions of chunk1, chunk1And2, chunk1Through3, chunk1Through4, etc. Store chunks in an array and join them after they are all loaded.
function getPage () {
http.get( 'some.url.com' , function (res) {
var chunks = [];
res.on('data', function (chunk) {
chunks.push(chunk);
});
res.on('end', function (err) {
$ = cheerio.load(chunks.join(''));
event.emit('pageLoaded');
});
});
}
Is there a way to clear the data used completely for every function call??
No. JavaScript is garbage collected and does not have a user-land API to impact the garbage collector's behavior in any way. Just don't leak variables or hold references to objects you no longer need and that's all you can do.