Created
January 20, 2013 05:05
-
-
Save mkazantsev/4576766 to your computer and use it in GitHub Desktop.
String Concatenation vs. Array.join() benchmarking for Node.js with Benchmark.js.
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'), | |
suite = Benchmark.Suite(); | |
var COUNT = process.argv[2] - 0; | |
console.log(COUNT); | |
var data = []; | |
for (var i = 0; i < COUNT; i++) { | |
data.push('string'); | |
} | |
suite.add('Plain concatenation', function() { | |
var res = ''; | |
for (var i = 0; i < COUNT; i++) { | |
res += data[i]; | |
} | |
}) | |
.add('String array join()', function() { | |
data.join('') | |
}) | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').pluck('name')); | |
}) | |
.run({ 'async': true }); |
Not fair..
For real string creation, you need '1 array per 1 string'.
And you don't need to save string into array and get string from array if you use string +=
@mkazantsev: your link is broken
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See more details: http://www.mkaz.ru/blog/2013/01/19/plain-string-concatenation-vs-array-dot-join/