Skip to content

Instantly share code, notes, and snippets.

@trevordixon
Created May 7, 2014 05:18
Show Gist options
  • Save trevordixon/070c7fb0d799fc2dda02 to your computer and use it in GitHub Desktop.
Save trevordixon/070c7fb0d799fc2dda02 to your computer and use it in GitHub Desktop.
Test speed of repeated require() calls
var iters = 100000;
// Many require calls
var start = process.hrtime();
for (var i = 0; i < iters; i++) {
require('fs');
}
console.log('Many require calls: ' + process.hrtime(start)[1]/1000000 + ' milliseconds');
// Many noop function calls
function noop() {};
var start = process.hrtime();
for (var i = 0; i < iters; i++) {
noop();
}
console.log('Many noop function calls: ' + process.hrtime(start)[1]/1000000 + ' milliseconds');
// Do nothing many times
var start = process.hrtime();
for (var i = 0; i < iters; i++) {
}
console.log('Do nothing many times: ' + process.hrtime(start)[1]/1000000 + ' milliseconds');
@trevordixon
Copy link
Author

On my laptop running node v0.10.26 on OS X, I got:

Many require calls: 49.85532 milliseconds
Many noop function calls: 0.926823 milliseconds
Do nothing many times: 0.576822 milliseconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment