Repeat "hoge" 100,000 times to connect.
String result = "";
for (var i = 0; i < 100000; i++) {
result += "Hoge";
}
StringBuilder
var builder = new StringBuilder();
for (var i = 0; i < 100000; i++) {
builder.append("Hoge");
}
var result = builder.toString();
Execution time (ms) | |
---|---|
+ Operator | 5709 |
StringBuilder | 9 |
Overwhelming StringBuilder victory. It seems that the method of concatenating with the + operator takes time because the String object is created / destroyed repeatedly internally.
Recommended Posts