A while back I was looking for a method in Java that allows me to repeat a string a certain number of times. There is a String constructor in .NET that implements this feature and I was hoping to find that in Java: no luck. The conversation ended up coming around to tricks we’ve seen in JavaScript.
Naive Approach
This first example is what I think many people would write as a first pass. This looks reasonable and is straight to the point. However, it is expensive to concatenate strings in a loop like that. The script engine has to allocate a new string for the concatenation, copy the old value into the new string, and copy the appended value into the string. You’ll really start seeing slow downs (O(n^2)) with this approach as the count increases
function repeatString(string, count) {
var buffer = “”;
for (var i = 0; i < count; i++) {
buffer += string;
}
return buffer;
}
Better Approach
This next example avoids the string allocation and copy issue by storing each string instance as an element within an array. We simply join the array using an empty string as a delimiter. The idea here is not only are we avoiding repeated allocations and copies, but we’re using the native join implementation for speed.
function repeatString(string, count) {
var buffer = [];
for (var i = 0; i < count; i++) {
buffer.push(string);
}
return buffer.join("");
}
Nice Hack This last example was suggested by Robin Debreuil. Keep in mind that if we can get the script engine to do the hard part for us, then we get a big speed advantage. Robin’s approach is to remove the loop. First we allocate an array that contains (count + 1) elements. Finally, we join using our string as the delimiter. Each element in the array is undefined, so they don’t contribute to the final string and since we have (buffer.length - 1) delimiters, we end up with (count + 1 - 1) or (count) instances of our string.
function repeatString(string, count) {
var buffer = new Array(count + 1);
return buffer.join(string);
}