Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does readFileSync of node.js cache the read files?

Does fs.readFileSync of node.js cache files that's been read?

I don't think it does because doc doesn't say so, but my code is behaving that way..

  • edit

Relevant part of code

     // after mainPath file is altered, it's not reflected until I restart node.js
     var scriptString =  fs.readFileSync(mainPath);
     var app = vm.createScript(scriptString, mainPath); 

     // Run the app

     app.runInContext(context);
like image 544
eugene Avatar asked Feb 27 '15 10:02

eugene


People also ask

Does NodeJS cache require?

Per the node documentation, modules are cached after the first time they are loaded (loaded is synonymous with 'required'). They are placed in the require. cache . This means that every future require for a previously loaded module throughout a program will load the same object that was loaded by the first require.

Where does NodeJS save files?

D:\myproject\subfoldername> node helloworld.js It is very easy.. Go to your command line. navigate to the file location.. then simply run the node helloworld.

How NodeJS read the content of a file?

To get the contents of a file as a string, we can use the readFileSync() or readFile() functions from the native filesystem ( fs ) module in Node. js. The readFileSync() function is used to synchronously read the contents from a file which further blocks the execution of code in Nodejs.


1 Answers

Reads are buffered during a given read operation. E.g. when you ask to read a byte, it will likely read many more bytes than a single byte into a buffer and then return that single byte to you. But beyond that, there is no caching built into node.js from one read of the file to another. And, if you're using readFileSync() to read the whole file at once, this buffering wouldn't affect you.

The OS itself will do caching underneath node.js, but that is usually write-through caching which is designed to save disk reads, but never have stale data.

like image 87
jfriend00 Avatar answered Sep 19 '22 14:09

jfriend00