0

From: book- "Nodejs in Action"

var server = http.createServer( function( req, res )
        {
            var url  = parse( req.url);
            var path = join( root, url.pathname);
            
            var stream = fs.createReadStream( path);

            stream.on( 'data', function( chunk)
                {
                   res.write( chunk);
                });

            stream.on( 'end', function()
                {
                    res.end();
                    });
        });

Why is it a good idea to read and send data in chunks rather than use 'readFile' and pass everything at once?

Why should we prefer to use createReadStream instead of readFile to server static files in nodejs?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

2 Answers2

2

Check out this answer https://stackoverflow.com/a/4591335/10961055

fs.readFile will load the entire file into memory as you pointed out, while as fs.createReadStream will read the file in chunks of the size you specify.

The client will also start receiving data faster using fs.createReadStream as it is sent out in chunks as it is being read, while as fs.readFile will read the entire file out and only then start sending it to the client. This might be negligible, but can make a difference if the file is very big and the disks are slow.

Think about this though, if you run these two functions on a 100MB file, the first one will use 100MB memory to load up the file while as the latter would only use at most 4KB.

Edit: I really don't see any reason why you'd use fs.readFile especially since you said you will be opening large files. By Christian Joudrey

Wael Zoaiter
  • 686
  • 1
  • 7
  • 21
1

Because the readFile method reads the file to the memory at once, hence taking more memory (because the file may be very big). On the other hand, createReadStream uses memory only for the current chunk it reads. This way you can operate large files with a relatively small amount of RAM. In other words, using createReadStream is more memory efficient.

Sergey
  • 1,000
  • 8
  • 19