Closed mfbx9da4 closed 1 year ago
Global RegExps are stateful through the .lastIndex
property. Exporting a function that gives consumers a fresh RegExp means they don't have to worry about this.
Also see https://github.com/mathiasbynens/emoji-regex/issues/19#issuecomment-303298828
Oooo TIL regex's in JS are stateful 🤯 god knows how many bugs I created without thinking about this
I noticed you've decided to use a factory pattern rather just a plain export. eg.
Presumably this was decided because there is a cost to the JS runtime compiling the regex upfront which doesn't need to happen if it's contained in the function, this would only happen when the function is called?
Would it make more sense to have a singleton API e.g
Or is the cost for compiling the regex only paid the first time the factory is called and subsequent calls are negligible?