Welcome Login
Blog Photos Links

RSS Feed

Regex is SLOW!

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.

John Andersen

I think you ran into the same problem I had a couple of years ago: http://www.john-james-andersen.com/blog/programming/javascript-when-in-doubt-dont-replace.html Keep the good content flowing...enjoy reading your findings!


What's New

There are currently no new items, please check back later.

Archives
2021 (2)
  September (1)
  May (1)
2019 (1)
  August (1)
2018 (3)
  August (1)
  April (1)
  January (1)
2017 (1)
  January (1)
2016 (4)
  December (1)
  November (1)
  May (1)
  January (1)
2015 (1)
  December (1)
2014 (2)
  August (1)
  February (1)
2013 (4)
  October (1)
  July (1)
  June (1)
  April (1)
2012 (11)
  December (2)
  October (3)
  September (1)
  May (1)
  April (1)
  February (2)
  January (1)
2011 (14)
  December (1)
  November (1)
  September (2)
  July (2)
  June (1)
  May (1)
  April (2)
  March (3)
  January (1)
2009 (2)
  October (1)
  June (1)
2008 (1)
  September (1)