O.o? LOTS OF RECURSION? Its O(N) where N is the number of substitutions. With a `g` you will still be O(N) with a smaller factor. The factor is large ONLY in cases where same variable is being used multiple times in a string. This is surely premature optimization.