LoginSignup
2
2

More than 5 years have passed since last update.

【メモ】文字列結合の結果時間比較

Posted at
var i, count = 100000;

var foo = [];
console.time("Array Join");
for(i = 0; i < count; ++i){
  foo.push("あいうえお");
}
foo.join('');
console.timeEnd("Array Join");


var bar = "";
console.time("String Concat");
for(i = 0; i < count; ++i){
  bar += "あいうえお";
}
console.timeEnd("String Concat");
上記コードの実行結果
Google Chrome Firefox
Array Join 125.000ms ~ 146.000ms 61.40ms ~ 82.99ms
String Concat 61.40ms ~ 82.99ms 60.85ms ~ 89.89ms

20回ほど実行してみて、その上下限の値を記載しています。
1年前ぐらいにやってみた時は配列を結合する方が早かった記憶がありますが…。

ともあれ、今は文字列結合が早いみたいですね。
ただ10回とかだと恐らくさほど変わらないと思うので、
普段は個人の好みか、プロジェクトのコーディング規則に従うかでしょうか。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2