Skip to content

Instantly share code, notes, and snippets.

@networkimprov
Created October 29, 2011 18:44
Show Gist options
  • Save networkimprov/1324921 to your computer and use it in GitHub Desktop.
Save networkimprov/1324921 to your computer and use it in GitHub Desktop.
Async filesystem ops vs. OS filesystem cache
var fs = require('fs');
var sMax = 500000;
var sDir = 'teststat';
var sCount = 2;
if (!fs.statSync(sDir)) {
fs.mkdirSync(sDir, 0700);
for (var a=0; a < 100; ++a)
fs.writeFileSync(sDir+'/'+a, 'some text');
}
var sT = Date.now();
for (var aC=0; aC < sCount; ++aC)
astat(0);
function astat(n) {
if (n < sMax/sCount) {
fs.stat(sDir+'/'+Math.floor(Math.random()*100), function(err, stats) {
if (err) throw err;
astat(++n);
});
return;
}
if (--aC === 0) {
console.log('async '+ (Date.now()-sT) +' ms');
sT = Date.now();
sstat(0);
}
}
function sstat(n) {
if (n < sMax) {
var stats = fs.statSync(sDir+'/'+Math.floor(Math.random()*100));
process.nextTick(function() { sstat(++n) });
return;
}
console.log('sync '+ (Date.now()-sT) +' ms');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment