2009/7/20 Ariel Flesler <afles...@gmail.com>: > I'm really aware of how the different interpreters work, but it seems to me > that immutable literals (strings, numbers, regexps) are the first thing I'd > cache in a constant. Maybe current interpreters do that already internally ? >
ECMA-262 3rd Edition section 7.8.5 [1] states: "A regular expression literal is an input element that is converted to a RegExp object (section 15.10) when it is scanned. The object is created before evaluation of the containing program or function begins. Evaluation of the literal produces a reference to that object; it does not create a new object." So inline regular expressions are not evaluated multiple times, they are turned into a RegExp object at compile time and become a reference to that object. This means that there is unlikely to be any benefit from assigning them to a variable, and if dereferencing that variable takes longer than simply dereferencing the RegExp object reference, then moving them to the variable would actually decrease performance. I would assume that this is something that all JS engines have implemented correctly, as it would take more work to _fail_ to implement it according to spec. Certainly, Microsoft's document "JScript Deviations from ES3" [2] has no mention of JScript's implementation violating this portion of the spec. RegExp objects created at runtime using the RegExp constructor would however benefit from caching; in fact I applied this technique in my own implementation of getElementsByClassName a couple of years ago. Cheers, Nick. [1] <http://www.ecma-international.org/publications/standards/Ecma-262.htm> [2] <http://wiki.ecmascript.org/lib/exe/fetch.php?id=resources%3Aresources&cache=cache&media=resources:jscriptdeviationsfromes3.pdf> -- Nick Fitzsimons http://www.nickfitz.co.uk/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "jQuery Development" group. To post to this group, send email to jquery-dev@googlegroups.com To unsubscribe from this group, send email to jquery-dev+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en -~----------~----~----~----~------~----~------~--~---