3

We are working on an extension to google-code-prettify which does the code-coloring for source-code on a webpage. We have a very long list of keywords (approx 4000) in Mathematica and while the performance is still very good, I wondered whether I can speed things up.

The regular expression for our keyword list looks like this

var keywords = 'AbelianGroup|Abort|AbortKernels|AbortProtect|Above|Abs|Absolute|\
AbsoluteCurrentValue|AbsoluteDashing|AbsoluteFileName|AbsoluteOptions|\
AbsolutePointSize|AbsoluteThickness|AbsoluteTime|AbsoluteTiming|AccountingForm';
new RegExp('^(?:' + keywords + ')\\b')

Can such an or-ed regex be made faster when it is compiled? Would it in the first place make sense to compile it, since google-code-prettify is a JavaScript running on the server. I don't know whether this script is loaded freshly every time a web-page is loaded. In this case, it is maybe not worth the overhead to compile it.

halirutan
  • 4,281
  • 18
  • 44
  • Presumably you _are_ assigning the regex to another variable and re-using it rather than calling `new RegExp` every time? – nnnnnn Jan 29 '12 at 09:34
  • I just split this for better readability. Look here https://github.com/halirutan/Mathematica-Source-Highlighting/blob/master/mathematica-source-highlighter.user.js – halirutan Jan 29 '12 at 09:40

1 Answers1

3
  1. google-code-prettify runs on the client (it's a script; the source is requested from the server by the browser).
  2. Creating the RegExp object does compile it, at runtime.

In other words, just leave it as-is.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • Ok, now as you say it seems obvious that the script has to run on the client since it is just fetched like the whole web-page. Additionally, I must have missed this post http://stackoverflow.com/a/884784/1078614 which would have answered my question. Anyway, thank you very much. – halirutan Jan 29 '12 at 12:35