{"id":15649331,"url":"https://github.com/jonschlinkert/regex-cache","last_synced_at":"2025-04-09T20:10:42.742Z","repository":{"id":27067908,"uuid":"30534152","full_name":"jonschlinkert/regex-cache","owner":"jonschlinkert","description":"Memoize the results of a call to the RegExp constructor, avoiding repetitious runtime compilation of the same string and options, resulting in dramatic speed improvements.","archived":false,"fork":false,"pushed_at":"2017-09-01T15:21:06.000Z","size":37,"stargazers_count":39,"open_issues_count":1,"forks_count":7,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-09T20:10:37.632Z","etag":null,"topics":["cache","javascript","regex","regular-expression"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonschlinkert.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-09T12:12:11.000Z","updated_at":"2021-12-02T14:01:51.000Z","dependencies_parsed_at":"2022-09-15T23:52:13.838Z","dependency_job_id":null,"html_url":"https://github.com/jonschlinkert/regex-cache","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fregex-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fregex-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fregex-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fregex-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonschlinkert","download_url":"https://codeload.github.com/jonschlinkert/regex-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248103872,"owners_count":21048245,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","javascript","regex","regular-expression"],"created_at":"2024-10-03T12:29:17.449Z","updated_at":"2025-04-09T20:10:42.712Z","avatar_url":"https://github.com/jonschlinkert.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 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\u0026label=Travis)](https://travis-ci.org/jonschlinkert/regex-cache) [![Windows Build Status](https://img.shields.io/appveyor/ci/jonschlinkert/regex-cache.svg?style=flat\u0026label=AppVeyor)](https://ci.appveyor.com/project/jonschlinkert/regex-cache)\n\n\u003e 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.\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n```sh\n$ npm install --save regex-cache\n```\n\n* Read [what this does](#what-this-does).\n* See [the benchmarks](#benchmarks)\n\n## Usage\n\nWrap a function like this:\n\n```js\nvar cache = require('regex-cache');\nvar someRegex = cache(require('some-regex-lib'));\n```\n\n**Caching a regex**\n\nIf 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:\n\n```js\nvar cache = require('regex-cache');\n\nfunction yourRegex(str, opts) {\n  // do stuff to str and opts\n  return new RegExp(str, opts.flags);\n}\n\nvar regex = cache(yourRegex);\n```\n\n## Recommendations\n\n### Use this when...\n\n* **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.\n* **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.\n\n### Do not use this when...\n\n* **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.\n\n### Example benchmarks\n\nPerformance results, with and without regex-cache:\n\n```bash\n# no args passed (defaults)\n  with-cache x 8,699,231 ops/sec ±0.86% (93 runs sampled)\n  without-cache x 2,777,551 ops/sec ±0.63% (95 runs sampled)\n\n# string and six options passed\n  with-cache x 1,885,934 ops/sec ±0.80% (93 runs sampled)\n  without-cache x 1,256,893 ops/sec ±0.65% (97 runs sampled)\n\n# string only\n  with-cache x 7,723,256 ops/sec ±0.87% (92 runs sampled)\n  without-cache x 2,303,060 ops/sec ±0.47% (99 runs sampled)\n\n# one option passed\n  with-cache x 4,179,877 ops/sec ±0.53% (100 runs sampled)\n  without-cache x 2,198,422 ops/sec ±0.47% (95 runs sampled)\n\n# two options passed\n  with-cache x 3,256,222 ops/sec ±0.51% (99 runs sampled)\n  without-cache x 2,121,401 ops/sec ±0.79% (97 runs sampled)\n\n# six options passed\n  with-cache x 1,816,018 ops/sec ±1.08% (96 runs sampled)\n  without-cache x 1,157,176 ops/sec ±0.53% (100 runs sampled)\n\n# \n# diminishing returns happen about here\n# \n\n# ten options passed\n  with-cache x 1,210,598 ops/sec ±0.56% (92 runs sampled)\n  without-cache x 1,665,588 ops/sec ±1.07% (100 runs sampled)\n\n# twelve options passed\n  with-cache x 1,042,096 ops/sec ±0.68% (92 runs sampled)\n  without-cache x 1,389,414 ops/sec ±0.68% (97 runs sampled)\n\n# twenty options passed\n  with-cache x 661,125 ops/sec ±0.80% (93 runs sampled)\n  without-cache x 1,208,757 ops/sec ±0.65% (97 runs sampled)\n\n# \n# when non-primitive values are compared\n# \n\n# single value on the options is an object\n  with-cache x 1,398,313 ops/sec ±1.05% (95 runs sampled)\n  without-cache x 2,228,281 ops/sec ±0.56% (99 runs sampled)\n```\n\n## Run benchmarks\n\nInstall dev dependencies:\n\n```bash\nnpm i -d \u0026\u0026 npm run benchmarks\n```\n\n## What this does\n\nIf 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.\n\nWhen 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.\n\nUsing 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**.\n\n## About\n\n### Contributing\n\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).\n\n### Contributors\n\n| **Commits** | **Contributor** |  \n| --- | --- |  \n| 31 | [jonschlinkert](https://github.com/jonschlinkert) |  \n| 1  | [MartinKolarik](https://github.com/MartinKolarik) |  \n\n### Building docs\n\n_(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.)_\n\nTo generate the readme, run the following command:\n\n```sh\n$ npm install -g verbose/verb#dev verb-generate-readme \u0026\u0026 verb\n```\n\n### Running tests\n\nRunning 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:\n\n```sh\n$ npm install \u0026\u0026 npm test\n```\n\n### Author\n\n**Jon Schlinkert**\n\n* [github/jonschlinkert](https://github.com/jonschlinkert)\n* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)\n\n### License\n\nCopyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).\nReleased under the [MIT License](LICENSE).\n\n***\n\n_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on September 01, 2017._","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonschlinkert%2Fregex-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonschlinkert%2Fregex-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonschlinkert%2Fregex-cache/lists"}