views:

84

answers:

2

When a regular expression is run JavaScript, is the regex engine that evaluates the expression compiled code? or the engine itself is written in javascript?

While doing some basic string matches tests, I found that a single regex is considerably faster than my JavaScript function that does the same thing, so I wondered why the regular expression was faster.

P.S: I'm totally new to regex.

+5  A: 

That would be up to implementation, but every implementation I know (such as Gecko, Trident), does so in compiled code.

EFraim
+3  A: 

As for if an implementation uses native code, it depends on the js engine.

You can influence your javascript code to be faster though.

If you're using the javascript regex syntax, using the /myregexhere/ syntax, the regular expression is compiled every time that code is executed. If you use the RegEx object in Javascript, you can compile your regular expressions and get better performance when using the same pattern many times.

Kekoa
The /myregexhere/ syntax should be compiled as well.
Matthew Crumley
If it is stored in a variable, if not, it is executed and compiled again.
Kekoa