create and cache a reusable function

I had nothing better to do, so I tried to optimize this a bit :-)

Instead of doing a replace and creating a lot of temporary functions, this creates one function from a string and reuses it.

I see speedups of 2x - 10x depending on the number of iterations.

So I would like some feedback on this :-)