4f24d23f2b07b244f661a66467e197c411cf34e24b043ecec41707584828bdab988df9d39602d4ce8c0942f57fc75d9e911ba16d6b3a27e3d714f26d3b1d7c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # regex-cache [![NPM version](https://img.shields.io/npm/v/regex-cache.svg?style=flat)](https://www.npmjs.com/package/regex-cache) [![NPM monthly downloads](https://img.shields.io/npm/dm/regex-cache.svg?style=flat)](https://npmjs.org/package/regex-cache) [![NPM total downloads](https://img.shields.io/npm/dt/regex-cache.svg?style=flat)](https://npmjs.org/package/regex-cache) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/regex-cache.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/regex-cache) [![Windows Build Status](https://img.shields.io/appveyor/ci/jonschlinkert/regex-cache.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/jonschlinkert/regex-cache)
  2. > Memoize the results of a call to the RegExp constructor, avoiding repetitious runtime compilation of the same string and options, resulting in surprising performance improvements.
  3. ## Install
  4. Install with [npm](https://www.npmjs.com/):
  5. ```sh
  6. $ npm install --save regex-cache
  7. ```
  8. * Read [what this does](#what-this-does).
  9. * See [the benchmarks](#benchmarks)
  10. ## Usage
  11. Wrap a function like this:
  12. ```js
  13. var cache = require('regex-cache');
  14. var someRegex = cache(require('some-regex-lib'));
  15. ```
  16. **Caching a regex**
  17. If you want to cache a regex after calling `new RegExp()`, or you're requiring a module that returns a regex, wrap it with a function first:
  18. ```js
  19. var cache = require('regex-cache');
  20. function yourRegex(str, opts) {
  21. // do stuff to str and opts
  22. return new RegExp(str, opts.flags);
  23. }
  24. var regex = cache(yourRegex);
  25. ```
  26. ## Recommendations
  27. ### Use this when...
  28. * **No options are passed** to the function that creates the regex. Regardless of how big or small the regex is, when zero options are passed, caching will be faster than not.
  29. * **A few options are passed**, and the values are primitives. The limited benchmarks I did show that caching is beneficial when up to 8 or 9 options are passed.
  30. ### Do not use this when...
  31. * **The values of options are not primitives**. When non-primitives must be compared for equality, the time to compare the options is most likely as long or longer than the time to just create a new regex.
  32. ### Example benchmarks
  33. Performance results, with and without regex-cache:
  34. ```bash
  35. # no args passed (defaults)
  36. with-cache x 8,699,231 ops/sec ±0.86% (93 runs sampled)
  37. without-cache x 2,777,551 ops/sec ±0.63% (95 runs sampled)
  38. # string and six options passed
  39. with-cache x 1,885,934 ops/sec ±0.80% (93 runs sampled)
  40. without-cache x 1,256,893 ops/sec ±0.65% (97 runs sampled)
  41. # string only
  42. with-cache x 7,723,256 ops/sec ±0.87% (92 runs sampled)
  43. without-cache x 2,303,060 ops/sec ±0.47% (99 runs sampled)
  44. # one option passed
  45. with-cache x 4,179,877 ops/sec ±0.53% (100 runs sampled)
  46. without-cache x 2,198,422 ops/sec ±0.47% (95 runs sampled)
  47. # two options passed
  48. with-cache x 3,256,222 ops/sec ±0.51% (99 runs sampled)
  49. without-cache x 2,121,401 ops/sec ±0.79% (97 runs sampled)
  50. # six options passed
  51. with-cache x 1,816,018 ops/sec ±1.08% (96 runs sampled)
  52. without-cache x 1,157,176 ops/sec ±0.53% (100 runs sampled)
  53. #
  54. # diminishing returns happen about here
  55. #
  56. # ten options passed
  57. with-cache x 1,210,598 ops/sec ±0.56% (92 runs sampled)
  58. without-cache x 1,665,588 ops/sec ±1.07% (100 runs sampled)
  59. # twelve options passed
  60. with-cache x 1,042,096 ops/sec ±0.68% (92 runs sampled)
  61. without-cache x 1,389,414 ops/sec ±0.68% (97 runs sampled)
  62. # twenty options passed
  63. with-cache x 661,125 ops/sec ±0.80% (93 runs sampled)
  64. without-cache x 1,208,757 ops/sec ±0.65% (97 runs sampled)
  65. #
  66. # when non-primitive values are compared
  67. #
  68. # single value on the options is an object
  69. with-cache x 1,398,313 ops/sec ±1.05% (95 runs sampled)
  70. without-cache x 2,228,281 ops/sec ±0.56% (99 runs sampled)
  71. ```
  72. ## Run benchmarks
  73. Install dev dependencies:
  74. ```bash
  75. npm i -d && npm run benchmarks
  76. ```
  77. ## What this does
  78. If you're using `new RegExp('foo')` instead of a regex literal, it's probably because you need to dyamically generate a regex based on user options or some other potentially changing factors.
  79. When your function creates a string based on user inputs and passes it to the `RegExp` constructor, regex-cache caches the results. The next time the function is called if the key of a cached regex matches the user input (or no input was given), the cached regex is returned, avoiding unnecessary runtime compilation.
  80. Using the RegExp constructor offers a lot of flexibility, but the runtime compilation comes at a price - it's slow. Not specifically because of the call to the RegExp constructor, but **because you have to build up the string before `new RegExp()` is even called**.
  81. ## About
  82. ### Contributing
  83. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  84. ### Contributors
  85. | **Commits** | **Contributor** |
  86. | --- | --- |
  87. | 31 | [jonschlinkert](https://github.com/jonschlinkert) |
  88. | 1 | [MartinKolarik](https://github.com/MartinKolarik) |
  89. ### Building docs
  90. _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
  91. To generate the readme, run the following command:
  92. ```sh
  93. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  94. ```
  95. ### Running tests
  96. Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
  97. ```sh
  98. $ npm install && npm test
  99. ```
  100. ### Author
  101. **Jon Schlinkert**
  102. * [github/jonschlinkert](https://github.com/jonschlinkert)
  103. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  104. ### License
  105. Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
  106. Released under the [MIT License](LICENSE).
  107. ***
  108. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on September 01, 2017._