Giter Club home page Giter Club logo

readlines-ng's Introduction

Tiny and fast async generator to read streams line by line

Build Status

Node 10+ is required. This package provides very simple but feature-reach functionality to consume readable streams or files line by line. It's also quite fast (up to 30% faster than readline) and has no dependencies.

Features:

  • reading of files (by name)
  • reading of readable streams
  • generator mode via readlines
  • readable stream mode via LineStream
  • customizable separator (default is \n)
  • raw bytes mode if encoding option is null
  • configurable lookahead buffer size

Documentation

Have a look at the index.js, it's really tiny. Or check the usage example below.

Usage examples:

const { readlines, LineStream } = require('readlines-ng');

// As a generator
for await (const line of readlines(filename, { encoding: 'utf8' })) {
  console.log(line);
}

// As a readable stream
const reader = new LineStream(filename, {
  chunkSize: 256*1024,
  encoding: null,
});
for await (const line of reader) {
  console.log(line);
}

// Custom separator
const { PassThrough } = require('stream');
const datasource = new PassThrough();
datasource.write('qux42');
datasource.write('foobar42');
datasource.end();

const reader = new LineStream(filename, { sep: '42' });
for await (const line of reader) {
  console.log(line);
}

Tests

npm run test

Benchmark

Trivial wc -l impl using this package exposes the performance better than the standard readline module.

npm run bench </path/to/huge/file>  # Node 11+ is required

# File size ~2.8 GB
> Benchmark: lines counter (readline module)
> 5974719 lines found
> Done in 5.757290349 sec

> Benchmark: lines counter (async generator)
> 5974719 lines found
> Done in 4.707053160 sec

> Benchmark: lines counter (async generator UTF-8)
> 5974719 lines found
> Done in 3.937784544 sec

> Benchmark: lines counter (readable stream)
> 5974719 lines found
> Done in 4.984460949 sec

> Benchmark: lines counter (readable stream UTF-8)
> 5974719 lines found
> Done in 4.354594750 sec

readlines-ng's People

Contributors

iximiuz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

therichu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.