1

Possible Duplicate:
Javascript: what's the point of RegExp.compile()?

Javascript is said to be an interpreted language, then how actually the compile method works for regular expressions. Does it is really compiles the pattern or it is just an abuse of notation.

Community
  • 1
  • 1
me_digvijay
  • 5,374
  • 9
  • 46
  • 83
  • 2
    The verb "compile" alone does not imply machine code. – James M Mar 29 '12 at 12:01
  • _"RegExp methods: The compile method is deprecated."_ – gdoron Mar 29 '12 at 12:02
  • @James McLaughlin: When I tried to find answer I found this article. http://www.w3schools.com/jsref/jsref_regexp_compile.asp The article itself says "The compile() method is used to compile a regular expression during execution of a script." – me_digvijay Mar 29 '12 at 12:08
  • On Chrome, I sometimes get different result when i first time use test after compile. For example, patt.test(x)->true, patt.test(x)->false, patt.test(x)->false, etc. And yes, I am not using the "g" flag. – zpavlinovic May 31 '12 at 23:10
  • compile was depreciated in JavaScript 1.5 – Rolf Apr 14 '14 at 03:06

1 Answers1

0

It's true that Javascript is an interpreted language, but all browsers handle Javascript differently. Google Chrome for example goes great lengths to compile JS code upon first execution; the underlying V8 engine translates JS into machine code to increase performance in huge web applications like Gmail.

Therefore Chrome compiles all JS code and not just regular expressions, maybe one could say that is an abuse of notation.

The Mozilla docs say that Firefox uses compilation of regular expressions, but then again, SpiderMonkey / TraceMonkey is a JIT compiler which generates bytecode.

I haven't found information about how the Internet Explorer handles things, I assume a little bit of everything, depending on the version number.

The really interesting question is: Why do you need this piece of information? If you want to optimize your Javascript code, I suggest to benchmark different approaches in all browsers you want to support and end up using the best-performing one. That should get you further than trying to understand internal browser functions which differ from version to version anyway.

Fabian
  • 4,160
  • 20
  • 32