3

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.

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

Comments

3

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.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.