Member-only story

Which reads files 10 times slower — Java or Node.js? Both

Node.js clearly excels at I/O operations but does not use the disk cache. Java gets fast when the files are the disk cache

Marian C.
5 min readJan 27, 2022

Node.js is believed to be efficient at disk or network operations because, unlike Java, in Node.js all I/O operations are non-blocking unless you intentionally choose a synchronous method. In Java disk or network operations are always blocking execution of the only or additional threads. If you want to make concurrent HTTP requests you will save time if you do not use Java.

In this post I compare how fast Node.js and Java read files. The efficiency of reading files might be important for some applications. If Node.js or Java is used to run a web server, they will have to read disk to serve static contents. Since the servers keep reading the same files, caching files improves the performance of the server. Another task that I routinely face is indexing many files. For such a task a file cache is not useful.

For my experiments I use a virtual machine with CentOS 8 on the Google cloud — a nice place for playing with processors. I first make measurements on a machine with 2 CPUs and 8 GB. Then I confirm the results on on the same machine but after increasing the number of processors to 16.

I created a folder /home/marian_caikovski/manyfiles with 20000 tiny files. Each file contains a single line of text. I execute a JavaScript that measures the time needed to loads all the files. Together with the loading time the script prints the total length of all the loaded contents and the number of the loaded files. The script repeats the measurement 10 times. I described this maximally straightforward unsophisticated script in detail in my previous post.

You see that for Node.js it takes about 2049 ms to read 428890 characters from 20000 files. The reading time is quite the same in all the 10 attempts. Absence of a dramatic improvement after the first read suggest that the files do not get cached.

--

--

Marian C.
Marian C.

Written by Marian C.

Java, JavaScript and SQL developer. Interested in data collection and visualization.

Responses (3)