Native Regex vs. UIWebView Regex

By Josh Wright on October 2011 in tips


My latest project has me doing A LOT of string manipulation which is a huge pain in objective-c. To top it off, I'd really like this part of my code to be dynamic so the logic could be updated without a full-on new version through the 5-7 day Apple approval process.

Normally I'd use RegexKitLite, but I started to wonder about the possibility of using a hidden UIWebView & just running JavaScript. This would let me dynamically load and run JavaScript. The code looks like this:

NSString* text = [JavaScript run:@"'Hello <b>World</b>!'.replace(/<[^>]*>/g, '')"];

You can see more of my automatic JavaScript runner at my iOS library on GitHub. Basically, it creates a hidden singleton UIWebView and runs the JavaScript against it.

So the question is, how fast is it? How much memory is that UIWebView taking up?

The answer is that it takes about 10x's as long and the web view takes about 90kb. You can see from my numbers below that that vast majority of the time increase is due to the overhead of running JavaScript - not due to the slowness of JavaScript.

Running each test 100,000 times resulted in the following:

//native 2+3:           0.000243 seconds
//native regex replace: 0.167785 seconds
//javascript 2+3:       2.693516 seconds
//javascript regex:     2.599044 seconds
//UIWebView:            80kb