Skip to content

Instantly share code, notes, and snippets.

@lsm
Created December 31, 2011 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsm/1544579 to your computer and use it in GitHub Desktop.
Save lsm/1544579 to your computer and use it in GitHub Desktop.
Compare Buffer and String
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