live-perf
v0.0.3
Published
A collection of ways to improve startup time, and measure time taken to require npm modules.
Downloads
3
Readme
live-perf
A collection of ways to improve startup time, and measure time taken to require npm modules.
const Perf = require('live-perf')
const perf = new Perf({
useDeduper: false,
useRequireTimes: false,
logRequires: false,
useRequireClock: false,
useTimeRequires: false,
})
perf.start()
// Your app's startup methods, etc.
perf.end({log: false})
API
perf.start()
Call this to install hooks and start tracking requires.
perf.end({log})
Call this to finish tracking and show summary information.
Options
useDeduper
WARNING: Currently the deduper adds more time than it saves. The issue is dynamically finding the correct version is expensive. I have isolated the hot function and caching should improve this.
Enable to dedupe modules at runtime.
This simply overrides Module._resolveFilename
, which is used to convert a require
request (e.g. require('lodash')
to an absolute path (e.g. /path/to/project/node_modules/lodash/index.js
).
Every time require
is used:
First, we get the version specified in the closest package.json file.
- If there is a top-level dep that satisfies, use that.
- If a satisfying module is present in
~/.live-perf-store/node_modules/.store
(a pnpm project), use that. - After this, we resolve symlinks, such that if more than one module is symlinked to the same module, they will both resolve to the same one.
useRequireTimes
Lists all modules that are required in your app, and all the different versions specified in package.json files.
logRequires
Print a log message everytime a module is required. For rough debugging.
useRequireClock
Lists all modules that are required in your app, and the files which require them, and whether they are being dynamically deduped.
useTimeRequires
When you ctrl+c
from your app it will use the time-requires
npm module
to show you a list of the modules taking the most time to be required.
Test
No tests at the moment.
Examples
Run: cd examples && node main
In examples/main/index.js
, toggle the following value to see what happens with and without deduping: const useDeduper = true
To capture a CPU profile to view in Chrome DevTools run cd examples && node --inspect main
and open the link printed in the console. Your profile will be viewable in the Profiles
tab.
Development
npm run gulp watch
Using Chrome Debugger
There is a native inspector in Node >6.3.
This allows you to profile your app in the Chrome DevTools.
cd examples && node --inspect main
Use --debug-brk
if you want to halt when the app starts.
To automatically capture a CPU profile on startup:
console.profile('my-profile')
require('./my-app')
console.profileEnd('my-profile')
Your profile will be viewable in the Profiles
tab.
This will mainly be useful to see how expensive the deduping code is.
See examples/main/index.js
for a full working example.