browserify / detective

Find all calls to require() no matter how deeply nested using a proper walk of the AST
Other
414 stars 60 forks source link

Optimization: Better word fast-path detection #48

Closed zertosh closed 9 years ago

zertosh commented 9 years ago

Just doing an indexOf check could still match "requires", "required", "requirejs", etc. By using a conservative regexp we can decrease the likelihood of a false positive. (And I think a cached regexp is faster than indexOf http://jsperf.com/substring-test/3).

cc: @substack


I had a better regexp for the common "require" case with no "isRequire" - which is why the test is so crazy. Let me know if you're open to it:

/(^|\W)require(\s*|\/\*[^*]*\*\/|\/\/[^\n]*)*\(/
   ^     ^    ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ^
   |     |    |                            |  |
   |     |    |                            |  + open parens 
   |     |    |                            v
   |     |    + (spaces | block comments | line comments) any number of times
   |   + our word
   + start or anything not [a-zA-Z_]
ghost commented 9 years ago

Another optimization I've thought about would be to use https://github.com/dominictarr/js-tokenizer instead of a full AST.