Skip to content

Instantly share code, notes, and snippets.

@ykzts
Created January 3, 2012 09:27
Show Gist options
  • Select an option

  • Save ykzts/1554248 to your computer and use it in GitHub Desktop.

Select an option

Save ykzts/1554248 to your computer and use it in GitHub Desktop.
(function() {
const url = require('url');
const http = require('http');
const fs = require('fs');
const DOWNLOAD_URI = process.argv[process.argv.length-1];
function main() {
var options = url.parse(DOWNLOAD_URI);
options.method = 'GET';
var req = http.request(options, function(res) {
var buffers = [];
var length = 0;
res.on('data', function(chunk) {
buffers.push(chunk);
length += chunk.length;
});
res.on('end', function() {
console.log('length: ' + length + ' bytes.');
test(buffers, length);
});
});
req.end();
}
function test(buffers, length) {
[test1, test2].forEach(function(func) {
var time = new Date();
for (var i=10000; i>=0; i--) {
func(buffers, length);
}
console.log(func.name + ': ' + (new Date() - time) + ' msec.');
});
}
function test1(buffers, length) {
var buffer = buffers.reduce(function(previousBuffer, currentBuffer) {
var buffer = new Buffer(previousBuffer.length + currentBuffer.length);
previousBuffer.copy(buffer);
currentBuffer.copy(buffer, previousBuffer.length);
return buffer;
});
}
function test2(buffers, length) {
var buffer = buffers.reduceRight(function(previousBuffer, currentBuffer) {
length -= currentBuffer.length;
currentBuffer.copy(previousBuffer, length);
return previousBuffer;
}, new Buffer(length));
}
if (require.main === module) {
main();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment