{"_id":"cache-require-paths","_rev":"278248","name":"cache-require-paths","description":"Caches resolved paths in module require to avoid Node hunting for right module. Speeds up app load.","dist-tags":{"latest":"0.3.0"},"maintainers":[{"name":"bahmutov","email":"gleb.bahmutov@gmail.com"}],"time":{"modified":"2021-06-03T18:08:56.000Z","created":"2015-04-22T12:48:00.790Z","0.3.0":"2016-08-23T20:38:30.145Z","0.2.0":"2016-02-06T04:39:53.443Z","0.1.2":"2015-05-04T13:21:11.412Z","0.1.1":"2015-04-26T03:38:26.935Z","0.1.0":"2015-04-22T12:48:00.790Z"},"users":{"chocolateboy":true},"author":{"name":"Gleb Bahmutov","email":"gleb.bahmutov@gmail.com"},"repository":{"type":"git","url":"git+https://github.com/bahmutov/cache-require-paths.git"},"versions":{"0.3.0":{"name":"cache-require-paths","description":"Caches resolved paths in module require to avoid Node hunting for right module. Speeds up app load.","main":"index.js","version":"0.3.0","scripts":{"test":"cd test; npm install; node index; node index --cache; node index --cache","semantic-release":"semantic-release pre && npm publish && semantic-release post","size":"t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";"},"repository":{"type":"git","url":"git+https://github.com/bahmutov/cache-require-paths.git"},"files":["index.js"],"keywords":["npm","node","application","load","startup","performance","speed","cache","resolution"],"author":{"name":"Gleb Bahmutov","email":"gleb.bahmutov@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/bahmutov/cache-require-paths/issues"},"homepage":"https://github.com/bahmutov/cache-require-paths","devDependencies":{"pre-git":"0.2.1","semantic-release":"^4.3.5"},"pre-commit":["npm test","npm version"],"gitHead":"6a6e7b55ad9616fe6b99d666974d1e127ef21226","_id":"cache-require-paths@0.3.0","_shasum":"12a6075a3e4988da4c22f218e29485663e4c4a63","_from":".","_npmVersion":"2.15.10","_nodeVersion":"4.5.0","_npmUser":{"name":"bahmutov","email":"gleb.bahmutov@gmail.com"},"dist":{"shasum":"12a6075a3e4988da4c22f218e29485663e4c4a63","size":3735,"noattachment":false,"key":"/cache-require-paths/-/cache-require-paths-0.3.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/cache-require-paths/download/cache-require-paths-0.3.0.tgz"},"maintainers":[{"name":"bahmutov","email":"gleb.bahmutov@gmail.com"}],"_npmOperationalInternal":{"host":"packages-16-east.internal.npmjs.com","tmp":"tmp/cache-require-paths-0.3.0.tgz_1471984707615_0.0527331733610481"},"directories":{},"publish_time":1471984710145,"_cnpm_publish_time":1471984710145,"_hasShrinkwrap":false},"0.2.0":{"name":"cache-require-paths","description":"Caches resolved paths in module require to avoid Node hunting for right module. Speeds up app load.","main":"index.js","version":"0.2.0","scripts":{"test":"cd test; npm install; node index; node index --cache; node index --cache","semantic-release":"semantic-release pre && npm publish && semantic-release post","size":"t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";"},"repository":{"type":"git","url":"git+https://github.com/bahmutov/cache-require-paths.git"},"files":["index.js"],"keywords":["npm","node","application","load","startup","performance","speed","cache","resolution"],"author":{"name":"Gleb Bahmutov","email":"gleb.bahmutov@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/bahmutov/cache-require-paths/issues"},"homepage":"https://github.com/bahmutov/cache-require-paths","devDependencies":{"pre-git":"0.2.1","semantic-release":"^4.3.5"},"pre-commit":["npm test","npm version"],"gitHead":"cf0324e87939607950b717c229f8ea17773e91f2","_id":"cache-require-paths@0.2.0","_shasum":"4c65ce5254be8daebf91af74f2bc59d28f1a3a19","_from":".","_npmVersion":"2.14.18","_nodeVersion":"4.2.6","_npmUser":{"name":"bahmutov","email":"gleb.bahmutov@gmail.com"},"dist":{"shasum":"4c65ce5254be8daebf91af74f2bc59d28f1a3a19","size":3628,"noattachment":false,"key":"/cache-require-paths/-/cache-require-paths-0.2.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/cache-require-paths/download/cache-require-paths-0.2.0.tgz"},"maintainers":[{"name":"bahmutov","email":"gleb.bahmutov@gmail.com"}],"_npmOperationalInternal":{"host":"packages-5-east.internal.npmjs.com","tmp":"tmp/cache-require-paths-0.2.0.tgz_1454733592311_0.7657208770979196"},"directories":{},"publish_time":1454733593443,"_cnpm_publish_time":1454733593443,"_hasShrinkwrap":false},"0.1.2":{"name":"cache-require-paths","version":"0.1.2","description":"Caches resolved paths in module require to avoid Node hunting for right module. Speeds up app load.","main":"index.js","scripts":{"test":"cd test; npm install; node index; node index --cache; node index --cache"},"repository":{"type":"git","url":"https://github.com/bahmutov/cache-require-paths.git"},"keywords":["npm","node","application","load","startup","performance","speed","cache","resolution"],"author":{"name":"Gleb Bahmutov","email":"gleb.bahmutov@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/bahmutov/cache-require-paths/issues"},"homepage":"https://github.com/bahmutov/cache-require-paths","devDependencies":{"pre-git":"0.2.1"},"pre-commit":["npm test","npm version"],"gitHead":"d329bffd8a0a4d56629f2ef29966f9af93ac9ee7","_id":"cache-require-paths@0.1.2","_shasum":"ec3f3cec489c7daf25bc2f7c22b26cbdd1a07571","_from":".","_npmVersion":"2.1.16","_nodeVersion":"0.11.10","_npmUser":{"name":"bahmutov","email":"gleb.bahmutov@gmail.com"},"maintainers":[{"name":"bahmutov","email":"gleb.bahmutov@gmail.com"}],"dist":{"shasum":"ec3f3cec489c7daf25bc2f7c22b26cbdd1a07571","size":3883,"noattachment":false,"key":"/cache-require-paths/-/cache-require-paths-0.1.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/cache-require-paths/download/cache-require-paths-0.1.2.tgz"},"directories":{},"publish_time":1430745671412,"_cnpm_publish_time":1430745671412,"_hasShrinkwrap":false},"0.1.1":{"name":"cache-require-paths","version":"0.1.1","description":"Caches resolved paths in module require to avoid Node hunting for right module. Speeds up app load.","main":"index.js","scripts":{"test":"cd test; node index; node index --cache; node index --cache"},"repository":{"type":"git","url":"https://github.com/bahmutov/cache-require-paths.git"},"keywords":["npm","node","application","load","startup","performance","speed","cache","resolution"],"author":{"name":"Gleb Bahmutov","email":"gleb.bahmutov@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/bahmutov/cache-require-paths/issues"},"homepage":"https://github.com/bahmutov/cache-require-paths","devDependencies":{"pre-git":"0.2.1"},"pre-commit":["npm test","npm version"],"gitHead":"8a5ea19558f117b6851d12926a839b1be0aac738","_id":"cache-require-paths@0.1.1","_shasum":"082acfd3abf446a03085d52d727f3a2e64bd2fd0","_from":".","_npmVersion":"1.4.28","_npmUser":{"name":"bahmutov","email":"gleb.bahmutov@gmail.com"},"maintainers":[{"name":"bahmutov","email":"gleb.bahmutov@gmail.com"}],"dist":{"shasum":"082acfd3abf446a03085d52d727f3a2e64bd2fd0","size":3740,"noattachment":false,"key":"/cache-require-paths/-/cache-require-paths-0.1.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/cache-require-paths/download/cache-require-paths-0.1.1.tgz"},"directories":{},"publish_time":1430019506935,"_cnpm_publish_time":1430019506935,"_hasShrinkwrap":false},"0.1.0":{"name":"cache-require-paths","version":"0.1.0","description":"Caches resolved paths in module require to avoid Node hunting for right module. Speeds up app load.","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"repository":{"type":"git","url":"https://github.com/bahmutov/cache-require-paths.git"},"keywords":["npm","node","application","load","startup","performance","speed","cache","resolution"],"author":{"name":"Gleb Bahmutov","email":"gleb.bahmutov@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/bahmutov/cache-require-paths/issues"},"homepage":"https://github.com/bahmutov/cache-require-paths","gitHead":"f1395eb9e7123db098ce084b272a30603eb7729c","_id":"cache-require-paths@0.1.0","_shasum":"5684775e30a928f8acdd59adf518b39a5b78fc71","_from":".","_npmVersion":"2.7.4","_nodeVersion":"0.12.2","_npmUser":{"name":"bahmutov","email":"gleb.bahmutov@gmail.com"},"maintainers":[{"name":"bahmutov","email":"gleb.bahmutov@gmail.com"}],"dist":{"shasum":"5684775e30a928f8acdd59adf518b39a5b78fc71","size":1592,"noattachment":false,"key":"/cache-require-paths/-/cache-require-paths-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/cache-require-paths/download/cache-require-paths-0.1.0.tgz"},"directories":{},"publish_time":1429706880790,"_cnpm_publish_time":1429706880790,"_hasShrinkwrap":false}},"readme":"# cache-require-paths\n\n> Caches resolved paths in module require to avoid Node hunting for right module. Speeds up app load.\n\n[![NPM][cache-require-paths-icon] ][cache-require-paths-url]\n\n[![Build status][cache-require-paths-ci-image] ][cache-require-paths-ci-url]\n[![semantic-release][semantic-image] ][semantic-url]\n\n[cache-require-paths-icon]: https://nodei.co/npm/cache-require-paths.png?downloads=true\n[cache-require-paths-url]: https://npmjs.org/package/cache-require-paths\n[cache-require-paths-ci-image]: https://travis-ci.org/bahmutov/cache-require-paths.png?branch=master\n[cache-require-paths-ci-url]: https://travis-ci.org/bahmutov/cache-require-paths\n[semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg\n[semantic-url]: https://github.com/semantic-release/semantic-release\n\nThis is a partial solution to Node \"hunting\" for right file to load when you require a 3rd party\ndependency. See [Node’s `require` is dog slow](https://kev.inburke.com/kevin/node-require-is-dog-slow/) \nand [Faster Node app require](http://glebbahmutov.com/blog/faster-node-app-require/) for details.\n\n## Use\n\n    npm install --save cache-require-paths\n\nLoad the module first in your application file\n\n```js\n// index.js\nrequire('cache-require-paths');\n...\n```\n\nThe first time the app loads, a cache of resolved file paths will be saved to `.cache-require-paths.json`\nin the current directory.  Every application startup after that will reuse this filename cache to avoid\n\"hunting\" for the right filename.\n\nTo save cached paths to a different file, set the environmental variable `CACHE_REQUIRE_PATHS_FILE`.\n\n## Results\n\nHere are results for loading common packages without and with caching resolved require paths.\nYou can run any of this experiments inside the `test` folder. `node index.js` loads\nusing the standard resolve. `node index.js --cache` uses a cache of the resolves paths.\n\nUsing node 0.10.37\n\n    require('X')    |  standard (ms)  |  with cache (ms)  |  speedup (%)\n    ------------------------------------------------------------------\n    express@4.12.3  |        72       |       46          |     36\n    karma@0.12.31   |       230       |      170          |     26\n    grunt@0.4.5     |       120       |       95          |     20\n    sails@0.11.0    |       170       |      120          |     29\n\nUsing node 0.12.2 - all startup times became slower.\n\n    require('X')    |  standard (ms)  |  with cache (ms)  |  speedup (%)\n    ------------------------------------------------------------------\n    express@4.12.3  |        90       |       55          |     38\n    karma@0.12.31   |       250       |      200          |     20\n    grunt@0.4.5     |       150       |      120          |     20\n    sails@0.11.0    |       200       |      145          |     27\n\n## TODO\n\n- [ ] Cache only the absolute paths (relative paths resolve quickly)\n- [ ] Invalidate cache if dependencies in the package.json change\n\n## Discussion\n\nYou can see Node on Mac OS X searchig for a file to load when loading an absolute path\nlike `require(express)` by using the following command to make a log of all system level\ncalls from Node (start this from another terminal before running node program)\n\n    sudo dtruss -d -n 'node' > /tmp/require.log 2>&1\n\nThen run the test program, for example in the `test` folder run\n\n    $ node index.js\n\nKill the `dtruss` process and open the generated `/tmp/require.log`. It shows every system call\nwith the following 4 columns: process id (should be single node process), relative time (microseconds),\nsystem call with arguments, and after the equality sign the numerical result of the call.\n\nWhen loading `express` dependency from the test program using `require('express');` we see\nthe following search (I abbreviated paths for clarity):\n\n    # microseconds call\n    664730 stat64(\".../test/node_modules/express\\0\", 0x7FFF5FBFECF8, 0x204)        = 0 0\n    664784 stat64(\".../test/node_modules/express.js\\0\", 0x7FFF5FBFED28, 0x204)         = -1 Err#2\n    664834 stat64(\".../test/node_modules/express.json\\0\", 0x7FFF5FBFED28, 0x204)       = -1 Err#2\n    664859 stat64(\".../test/node_modules/express.node\\0\", 0x7FFF5FBFED28, 0x204)       = -1 Err#2\n    664969 open(\".../test/node_modules/express/package.json\\0\", 0x0, 0x1B6)        = 11 0\n    664976 fstat64(0xB, 0x7FFF5FBFEC38, 0x1B6)         = 0 0\n    665022 read(0xB, \"{\\n  \\\"name\\\": \\\"express\\\", ...}\", 0x103D)        = 4157 0\n    665030 close(0xB)      = 0 0\n\nBy default, Node checks if the local `node_modules/express` folder exists first (first `stat64` call),\nThen it tries to check the status of the `node_modules/express.js` file and fails. \nThen `node_modules/express.json` file. Then `node_modules/express.node` file. Finally it opens\nthe `node_modules/express/package.json` file and reads the contents. \n\nNote that this is not the end of the story. Node loader only loads `express/package.json` to fetch\n`main` filename or use the default `index.js`! Each wasted file system call takes only 100 microseconds,\nbut the tiny delays add up to hundreds of milliseconds and finally seconds for larger frameworks.\n\nProfile the same program with `--cache` option added to the command line arguments\n\n    $ node index.js --cache\n\nThis option loads the `cache-require-paths` module as the first require of the application\n\n```js\nvar useCache = process.argv.some(function (str) {\n  return str === '--cache';\n});\nif (useCache) {\n  console.log('using filename cache');\n  require('cache-require-paths');\n}\n```\n\nThe trace now shows *no calls to find `express` package*, just straight load of the `express/index.js` file.\n\n    643466 stat64(\".../node_modules/express/index.js\\0\", 0x7FFF5FBFED28, 0x3)         = 0 0\n    643501 lstat64(\".../node_modules\\0\", 0x7FFF5FBFED08, 0x3)         = 0 0\n    643513 lstat64(\".../node_modules/express\\0\", 0x7FFF5FBFED08, 0x3)         = 0 0\n    643523 lstat64(\".../node_modules/express/index.js\\0\", 0x7FFF5FBFED08, 0x3)        = 0 0\n    643598 open(\".../node_modules/express/index.js\\0\", 0x0, 0x1B6)        = 12 0\n    643600 fstat64(0xC, 0x7FFF5FBFED58, 0x1B6)         = 0 0\n\nMission achieved. Note that the speedup only happens after the first application run finishes successfully.\nThe resolution cache needs to be saved to a local file, and this happens only on process exit.\n\n## Small print\n\nAuthor: Gleb Bahmutov &copy; 2015\n\n* [@bahmutov](https://twitter.com/bahmutov)\n* [glebbahmutov.com](http://glebbahmutov.com)\n* [blog](http://glebbahmutov.com/blog)\n\nLicense: MIT - do anything with the code, but don't blame me if it does not work.\n\nSpread the word: tweet, star on github, etc.\n\nSupport: if you find any problems with this module, email / tweet /\n[open issue](https://github.com/bahmutov/cache-require-paths/issues) on Github\n","_attachments":{},"homepage":"https://github.com/bahmutov/cache-require-paths","bugs":{"url":"https://github.com/bahmutov/cache-require-paths/issues"},"license":"MIT"}