Latest writing

Recursive string repeat

Following on from Axel's mind blowing string repeat function, I decided to see if I could use recursion to implement the same functionality.

Turned out to be quite easy. I did it with TDD, while remaining faithful to Axel's implementation. I've pushed the code to github under connrs/recursive-string-repeat but I will replicate the function here:

function repeat(num, str) {
    num = Number(num);

    if (num === 0) {
        return '';

    return repeat(num >>> 1, str + str) + (num & 1 ? str : '');

I know that JS perf testing isn't really important for something like this, but I wanted to do it as part of the experiment. Here are the test results compared to Axel's version. Looks like for loops win!

I must admit that it was quite fun to try this out.

Last moment test

I just wanted to be convinced that Google+ was truly indexing me at this speed. It would be nice if they'd actually display this activity in my timeline!