Created
January 3, 2012 09:27
-
-
Save ykzts/1554248 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| (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