Time Efficiency

Learn about time efficiency in streams with an example.

Time efficiency in streams

Let’s now consider the case of an application that compresses a file and uploads it to a remote HTTP server, which, in turn, decompresses it and saves it on the filesystem. If the client component of our application is implemented using a buffered API, the upload will start only when the entire file has been read and compressed.

On the other hand, the decompression will start on the server only when all the data has been received. A better solution to achieve the same result involves the use of streams. On the client machine, streams allow us to compress and send the data chunks as soon as they’re read from the filesystem, whereas on the server, they allow us to decompress every chunk as soon as it’s received from the remote peer. Let’s demonstrate this by building the application that we mentioned earlier, starting from the server side.

Let’s create a module named gzip-receive.js containing the following code:

Get hands-on with 1200+ tech skills courses.