Security News
OpenJS: “XZ Utils Cyberattack Likely Not an Isolated Incident”
OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.
fast-boot
Advanced tools
Weekly downloads
Readme
When Node.js starts, a lot of the time is wasted on loading module files. Of the module loading time, most of the work is
actually searching for the module file on the filesystem. When you require a module, node will look for it at the node_modules
folder relative to the requesting module. If not found there, it will start stepping up the folder hierarchy looking
for the module in each of the parent folders node_modules
folder.
This search scheme makes node.js do a lot fs.statSync
operations - an operation that throws an exception is the module file
does not exist.
Fast boot caches the location of module files those speeding the loading of the node.js process. By caching the locations, we reduce the number of io operations significantly.
For example, loading of an Express application (with no other modules) without fast-boot compared to with fast-boot -
| without fast-boot | with fast-boot
----------------- | ------------------- | --------------- fs.statSync | 713 | 0 fs.readFileSync | 152 | 62 fs.existsSync | 7 | 103 loading time | 93 mSec | 60 mSec
Note that the loading times are measured on a Mac Pro with SSD - on an actual VM running on hardware with spin disk the loading times will be considerably larger.
The module hooks into the node module 'module' and wraps the _resolveFilename
method, caching the files it finds
in order to improve node loading performance. Node does tons of file lookups as it resolves modules and does not cache
the found file locations.
fast-boot caches only modules located in the the projects node_modules
directory.
fast-boot supports two patterns of working:
The first time the application runs, fast-boot learns what modules are loaded and saves the list of those modules as a cache file (located by default in the tmp folder). The second time the application loads it uses the cache file and loads faster. It is assumed the cache file is located on a read/write enabled drive.
We can also improve the performance of the first time the application loads using a startup file. The startup file
is assumed to be created using a build step and distributed with the application sources, assumed to be read-only.
To create the startup file, load the application with all the modules and call saveStartupList()
. By default,
the startup file is saved in the project node_modules
folder.
Once the application loads using the startup file, any additional modules loaded not in the startup file will cause fast-boot to save a cache file in tmp.
Starts the caching
var nodeModuleCache = require("fast-boot");
nodeModuleCache.start(opts);
Start accepts an options parameter with two options
cacheFile
- alternate cache file location. Defaults to {os.tmpdir()}/module-locations-cache.json
startupFile
- alternate startup file location. Defaults to ./node_modules/module-locations-cache.json
, relative to the process.cwd()
cacheKiller
- used to invalidate the cache. Normally one will pass the application version number assuming that a different versionstatusCallback
- callback function called each time fast boot loads or saves. function(message)
may have different version of dependencies making modules located in different locations. The default is the version number from package.json,
if one existsstops the module
nodeModuleCache.stop();
saves the cache file. Callback is called after completed save with signature function(err) {}
nodeModuleCache.saveCache();
saves the startup file. Callback is called after completed save with signature function(err) {}
nodeModuleCache.saveStartupList();
reloads the modules list from the cache file (if exists) or the startup file (if exists)
nodeModuleCache.loadModuleList();
returns a statistics object about the caching effectiveness. The stats object include the following members
var stats = nodeModuleCache.stats();
console.log(stats.cacheHit);
console.log(stats.cacheMiss);
console.log(stats.notCached);
FAQs
Speeding up node boot time by caching for filesystem location of node modules
The npm package fast-boot receives a total of 110 weekly downloads. As such, fast-boot popularity was classified as not popular.
We found that fast-boot demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.
Company News
Come meet the Socket team at BSidesSF and RSA! We're sponsoring several fun networking events and we would love to see you there.
Security News
OSI is starting a conversation aimed at removing the excuse of the SaaS loophole for companies navigating licensing and the complexities of doing business with open source.