RegExpCache.js 669 B

1234567891011121314151617181920
  1. import LRUCache from './LRUCache.js'
  2. // A cache for frequently used country-specific regular expressions. Set to 32 to cover ~2-3
  3. // countries being used for the same doc with ~10 patterns for each country. Some pages will have
  4. // a lot more countries in use, but typically fewer numbers for each so expanding the cache for
  5. // that use-case won't have a lot of benefit.
  6. export default class RegExpCache {
  7. constructor(size) {
  8. this.cache = new LRUCache(size)
  9. }
  10. getPatternForRegExp(pattern) {
  11. let regExp = this.cache.get(pattern)
  12. if (!regExp) {
  13. regExp = new RegExp('^' + pattern)
  14. this.cache.put(pattern, regExp)
  15. }
  16. return regExp
  17. }
  18. }