UNPKG

motion

Version:

motion - moving development forward

84 lines (83 loc) 7.3 kB
{ "_args": [ [ "regex-cache@https://registry.npmjs.org/regex-cache/-/regex-cache-0.4.2.tgz", "/Users/nw/flint/packages/flint" ] ], "_from": "regex-cache@>=0.4.2 <0.5.0", "_id": "regex-cache@0.4.2", "_inCache": true, "_location": "/regex-cache", "_phantomChildren": {}, "_requested": { "name": "regex-cache", "raw": "regex-cache@https://registry.npmjs.org/regex-cache/-/regex-cache-0.4.2.tgz", "rawSpec": "https://registry.npmjs.org/regex-cache/-/regex-cache-0.4.2.tgz", "scope": null, "spec": "https://registry.npmjs.org/regex-cache/-/regex-cache-0.4.2.tgz", "type": "remote" }, "_requiredBy": [ "/micromatch" ], "_resolved": "https://registry.npmjs.org/regex-cache/-/regex-cache-0.4.2.tgz", "_shasum": "6e4f89c266bc03c33fd129c062184687f4663487", "_shrinkwrap": null, "_spec": "regex-cache@https://registry.npmjs.org/regex-cache/-/regex-cache-0.4.2.tgz", "_where": "/Users/nw/flint/packages/flint", "author": { "name": "Jon Schlinkert", "url": "https://github.com/jonschlinkert" }, "bugs": { "url": "https://github.com/jonschlinkert/regex-cache/issues" }, "dependencies": { "is-equal-shallow": "^0.1.1", "is-primitive": "^2.0.0" }, "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.", "devDependencies": { "benchmarked": "^0.1.4", "chalk": "^1.0.0", "micromatch": "^2.1.0", "mocha": "^2.1.0", "should": "*" }, "engines": { "node": ">=0.10.0" }, "files": [ "index.js" ], "homepage": "https://github.com/jonschlinkert/regex-cache", "keywords": [ "cache", "expression", "regex", "regexp", "regular", "regular expression", "store", "to-regex" ], "license": { "type": "MIT", "url": "https://github.com/jonschlinkert/regex-cache/blob/master/LICENSE" }, "main": "index.js", "name": "regex-cache", "optionalDependencies": {}, "readme": "# regex-cache [![NPM version](https://badge.fury.io/js/regex-cache.svg)](http://badge.fury.io/js/regex-cache) [![Build Status](https://travis-ci.org/jonschlinkert/regex-cache.svg)](https://travis-ci.org/jonschlinkert/regex-cache) \n\n> 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.\n\n- Read [what this does](#what-this-does).\n- See [the benchmarks](#benchmarks)\n\n## Install with [npm](npmjs.org)\n\n```bash\nnpm i regex-cache --save\n```\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\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 && npm run benchmarks\n```\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\n## Run tests\n\nInstall dev dependencies:\n\n```bash\nnpm i -d && npm test\n```\n\n## Contributing\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/regex-cache/issues)\n\n## Author\n\n**Jon Schlinkert**\n \n+ [github/jonschlinkert](https://github.com/jonschlinkert)\n+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) \n\n## License\nCopyright (c) 2015 Jon Schlinkert \nReleased under the MIT license\n\n***\n\n_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on March 25, 2015._\n\n[mentions-regex]: https://github.com/regexps/mentions-regex", "readmeFilename": "README.md", "repository": { "type": "git", "url": "git://github.com/jonschlinkert/regex-cache.git" }, "scripts": { "benchmarks": "node benchmark", "test": "mocha" }, "version": "0.4.2" }