Created
December 31, 2011 17:01
-
-
Save lsm/1544579 to your computer and use it in GitHub Desktop.
Compare Buffer and String
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
var Benchmark = require('benchmark'); | |
var suite = new Benchmark.Suite; | |
var dataString = ['xxx', 'ssss', 'yyyyy', '1121212', 'aaaaa']; | |
var dataBuffer = [new Buffer('xxx'), new Buffer('ssss'), new Buffer('yyyyy'), new Buffer('1121212'), new Buffer('aaaaa')]; | |
suite.add('Buffer#copy', function() { | |
var offset = 0; | |
dataBuffer.forEach(function(chunk) { | |
var buffBuffer = new Buffer(24); | |
chunk.copy(buffBuffer, offset); | |
offset += chunk.length; | |
}) | |
}) | |
.add('String#copy', function() { | |
var buffString = ''; | |
dataString.forEach(function(chunk) { | |
buffString += chunk; | |
}) | |
}) | |
.on('cycle', function(event, bench) { | |
console.log(String(bench)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').pluck('name')); | |
}) | |
// run async | |
.run({ 'async': false }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment