Skip to content

Instantly share code, notes, and snippets.

@yorkie
Created December 7, 2013 05:19
Show Gist options
  • Save yorkie/7837575 to your computer and use it in GitHub Desktop.
Save yorkie/7837575 to your computer and use it in GitHub Desktop.
benchmark for array.join and +
function plus(count) {
var s = 'apple';
var start = Date.now();
var result = '';
for (var i=0; i<count; i++) result += s;
console.log(Date.now()-start)
}
function concat(count) {
var s = 'apple';
var start = Date.now();
var result = [];
for (var i=0; i<count; i++) result[i] = s;
result.join('');
console.log(Date.now()-start)
}
var c = 1000000;
plus(c);
concat(c);

c = 100:

plus: 12ms

concat: 0ms

c = 10*000:

plus: 14ms

concat: 1ms

c = 100*1000

plus: 23ms

concat: 12ms

c = 500*1000

plus: 70ms !!!

concat: 70ms !!!

c = 1010001000

plus: 1355ms

concat: 1657ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment