in reply to Regex combining /(foo|bar)/ slower than using foreach (/foo/,/bar/) ???  

I believe using regex's - even for relatively simple matching - is expensive compared with the simpler (less elaborate) search builtins like index, or even comparison operators. As you know, index is an optimised function specifically created to find a substring within a string. But using a regex fires up the 'regex engine', which in itself is reasonably optimised, but can do a LOT more than the simpler functions and operators. As such it uses more processor resources.

UPDATE Just realized you were comparing it with /foo/ and /bar/, which are still regex's. Well, it is possible that the regex engine can optimize a regex with a constant matching expression (like /foo/) to the point where it is much faster than alternation (/(foo|bar)/). To really be able to answer that question, you'd need to know a fair bit about the internals of the regex engine.

UPDATE 2 See Roy Johnson's reply below. His benchmark verifies my guess :)

  • Comment on Re: Regex combining /(foo|bar)/ slower than using foreach (/foo/,/bar/) ???