Reading line-by-line file in JavaScript on client side

Eventually I’ve created new line-by-line reader, which is totally different from previous one.

Features are:

  • Index-based access to File (sequential and random)
  • Optimized for repeat random reading (milestones with byte offset saved for lines already navigated in past), so after you’ve read all file once, accessing line 43422145 will be almost as fast as accessing line 12.
  • Searching in file: find next and find all.
  • Exact index, offset and length of matches, so you can easily highlight them

Check this jsFiddle for examples.

Usage:

// Initialization
var file; // HTML5 File object
var navigator = new FileNavigator(file);

// Read some amount of lines (best performance for sequential file reading)
navigator.readSomeLines(startingFromIndex, function (err, index, lines, eof, progress) { ... });

// Read exact amount of lines
navigator.readLines(startingFromIndex, count, function (err, index, lines, eof, progress) { ... });

// Find first from index
navigator.find(pattern, startingFromIndex, function (err, index, match) { ... });

// Find all matching lines
navigator.findAll(new RegExp(pattern), indexToStartWith, limitOfMatches, function (err, index, limitHit, results) { ... });

Performance is same to previous solution. You can measure it invoking ‘Read’ in jsFiddle.

GitHub: https://github.com/anpur/client-line-navigator/wiki

Leave a Comment