javascript - Is readFileSync() during initialization of a Node.js web app a bad approach? -


there few static html-snippet files conditionally include in web pages node app serves. thinking of having cache map these snippets, include @ top of express's app.js file:

var cache = object.create(null); cache['404'] = fs.readfilesync('path/to/404.html'); cache['weekend'] = fs.readfilesync('path/to/weekend.html'); 

this code runs during initialization (i.e. whenever node app (re)started). wonder if using readfilesync() in context bad approach.


for comparison, analogous async approach:

var cache = object.create(null); fs.readfile('path/to/404.html', function (err, data) {     cache['404'] = data; }); fs.readfile('path/to/weekend.html', function (err, data) {     cache['weekend'] = data; }); 

in long-running program, performing synchronous request on startup fine. problem synchronous request block entire application. while individual requests suffer if have wait 0.1 seconds response, whole application not suffer if takes 0.1 second start up. (i'm assuming file isn't "huge").

to put way: benefit of async there other stuff can happening while io blocking call takes place. if entire program:

readfileasync(file, function() {     startlisteningforrequests(); } 

then it's equivalent reading sync. however, async version may more maintainable if need read 1000 files on startup - want happen in parallel. in practical experience web developer, that's unlikely, sync fine.


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -