Performance comparison in Node.js for reading large files: indexOf vs. byte-by-byte checking

In software development, choosing the most efficient method to parse large datasets is crucial for performance. I conducted an experiment to compare two different approaches for counting line breaks in a large text file using Node.js: using indexOf and manual byte-by-byte checking.

Experiment setup

  • Objective: Count the number of newline characters (\n, ASCII value 10) in a 4.92GB text file.
  • Method 1: Using indexOf to find newline characters.
  • Method 2: Manually checking each byte for the newline character.
  • Environment: Node.js stream processing on a text file with approximately 30,035,612 lines.

Method 1: Using indexOf

readStream.on('data', (chunk: Buffer) => {
    let index = chunk.indexOf(10); // Find the first occurrence of \n
    while (index !== -1) {
        index = chunk.indexOf(10, index + 1); // Find the next occurrence

Method 2: Byte-by-byte checking

readStream.on('data', (chunk: Buffer) => {
    for (let i = 0; i < chunk.length; i++) {
        if (chunk[i] === 10) {


  • Using indexOf: Averaged 7499.8ms over five runs (7685ms, 7526ms, 7398ms, 7535ms, 7355ms).
  • Byte-by-byte checking: Averaged 9556.2ms over five runs (9565ms, 9469ms, 9449ms, 9604ms, 9694ms).


The indexOf method was approximately 10-20% faster than manual byte-by-byte checking. This performance difference could be attributed to:

  1. Internal optimizations in the indexOf method.
  2. Reduced function call overhead in indexOf compared to byte-by-byte checking.
  3. Processor prediction and caching mechanisms potentially favoring the indexOf method.


This experiment highlights the importance of method selection in processing large datasets. Even seemingly minor optimizations can lead to significant performance improvements. Practical performance testing is essential, as theoretical efficiency and actual results can vary.