JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 48
  • Score
    100M100P100Q87817F
  • License MIT

High-performance paginated file reader for Node.js. Efficiently process large text files without loading them into memory.

Package Exports

  • readline-pager

Readme

๐Ÿ“„ readline-pager

logo

version downloads stars

โšก High-performance paginated file reader for Node.js. Efficiently process large text files without loading them into memory.

  • ๐Ÿ“ฆ Zero dependencies
  • โšก Up to ~3ร— faster than Node.js readline
  • ๐Ÿš€ Up to ~6ร— faster with optional native C++ acceleration
  • ๐Ÿ” Async (for await...of) and sync (for...of) iteration
  • ๐Ÿ“„ Page-based reading with manual control (next, nextSync)
  • ๐Ÿ”€ Forward and backward reading support
  • ๐Ÿงช Fully typed with high test coverage (>90%)

Important:
Performance depends heavily on the chunkSize option. Tune it for your storage device. A value of 64 KiB is usually a good starting point. Increasing it may improve throughput until you reach the best value for your hardware.


๐Ÿ“ฆ Installation

npm install readline-pager

๐Ÿš€ Quick start

import { createPager } from "readline-pager";

for await (const page of createPager("./bigfile.txt")) {
  console.log(page[0]);
}

Other usage patterns

import { createPager, createNativePager } from "readline-pager";

// Sync iteration
for (const page of createPager("./bigfile.txt")) {
}

// Manual async
const pager = createPager("./bigfile.txt");
while (true) {
  const page = await pager.next();
  if (!page) break;
}

// Manual sync
let page;
const pager = createPager("./bigfile.txt");
while ((page = pager.nextSync()) !== null) {}

// Native C++ (fastest)
for await (const page of createNativePager("./bigfile.txt")) {
}

โš™๏ธ Options

createPager(filepath, {
  chunkSize?: number,   // default: 64 * 1024 (64 KiB)
  pageSize?: number,    // default: 1_000
  delimiter?: string,   // default: "\n"
  prefetch?: number,    // default: 8
  backward?: boolean,   // default: false
  useWorker?: boolean,  // default: false
  tryNative?: boolean,  // default: true
});
  • chunkSize โ€” number of bytes read per I/O operation.
  • pageSize โ€” number of lines per page.
  • delimiter โ€” line separator.
  • prefetch โ€” maximum number of pages buffered internally.
  • backward โ€” read the file from end to start.
  • useWorker โ€” offload reading to a worker thread (forward reading only).
  • tryNative โ€” attempts to use the native reader, falls back to the non-native version if it fails.

Note: createNativePager supports only pageSize, delimiter, and backward options and does not support multi-character delimiters.


๐Ÿ“š API

pager.next(): Promise<string[] | null>

Returns the next page asynchronously.

Returns null when the end of the file is reached.

Empty lines are preserved.

Note: Unlike Node.js readline, which may skip empty files or leading empty lines, readline-pager always returns all lines.

  • A completely empty file (0 bytes) produces [""] on the first read.
  • A file containing multiple empty lines returns each line as an empty string.

pager.nextSync(): string[] | null

Synchronous version of pager.next().

Returns the next page immediately or null when the end of the file is reached.

pager.close(): Promise<void>

Stops reading and releases resources asynchronously. Safe to call at any time.


๐Ÿ“Š Benchmark

Run the benchmark locally:

npm run benchmark:node

# or customize with args
node test/_benchmark.ts --lines=20000 --page-size=500 --backward

Test setup: generated text files (UUID lines), NVMe SSD, Node.js runtime. Results are averaged across multiple runs. Actual performance depends on hardware.


โšก Throughput (MB/s)

Method 1M lines (35 MB) 10M lines (353 MB) 100M lines (3.5 GB) 1B lines (35.3 GB)
readline ~370 MB/s ~460 MB/s ~460 MB/s ~460 MB/s
readline-pager (JS) ~1100 MB/s ~1300 MB/s ~1300 MB/s ~1150 MB/s
readline-pager (C++) ~2200 MB/s ~2500 MB/s ~2500 MB/s ~2450 MB/s

๐Ÿ›  Development & Contributing

  • Minimum supported Node.js: v18.12
  • Development/test environment: Node v25.8 and TypeScript v6.0

Run tests:

npm i
npm test

Contributions are welcome. Open an issue or submit a PR.


๐Ÿ“œ License

MIT โ€” ยฉ Morteza Jamshidi