April 8, 2013, 6:09 pm - James Farrer
I had an interesting experience today with a script in ServiceNow (server side javascript) that was replacing variables in a large string (~28000 characters). We were using a regex to find and replace all occurances of 10-15 variables in the string. When I first wrote the code to do the replacing the string we were using was only a few lines and even as it started to grow we didn't notice any speed issues but then we added a large chunk of text and wow! We went from a second or two up to about 30 seconds of processing just to do this replacing.
I was honestly a bit surprised at how long it took to process. I tried a few things to see what I could do to speed it up and was almost to the point of setting up a painful caching process when I decided to try my hand at manually writing the replacement script. Boy am I glad I did!
In the string the variables I am replacing are nicely bracketed in the format of "[[var_name]]". So I took the string and used the split function on it with "[[" as the delimiter. Then I took each row (minus the first that wouldn't have a variable in it) and split it again on the ending portion of "]]". Then I used a really simple associative array lookup with the first part of the second array and rebuilt the string with the results.
At first I couldn't believe the results, but after testing and verifying, I found that it was taking longer to log results than it was taking to actually do the processing. That's an improvement from 30,000+ ms down to about 10 ms.
Now I thought Regex might not be incredibly fast, but this truly amazed me. Not only is the syntax a bit cryptic, but it's slow. Next time I'll definitely think twice before using a regex.