Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read FileReader object line-by-line without loading the whole file into RAM

Now that many browsers support reading local files with HTML5's FileReader, this opens the door to websites which go beyond 'database front-ends' into scripts which can do something useful with local data without having to send it up to a server first.

Pre-processing images and video before upload aside, one big application of FileReader would be loading data from some kind of on-disk table (CSV, TSV, whatever) into the browser for manipulation - perhaps for plotting or analysis in D3.js or creating landscapes in WebGL.

Problem is, most examples out there on StackOverflow and other sites use FileReader's .readAsText() property, which reads the whole file into RAM before returning a result.

javascript: how to parse a FileReader object line by line

To read a file without loading the data into RAM, one would need to use .readAsArrayBuffer(), and this SO post is the closest I can get to a good answer:

filereader api on big files

However, it's a bit too specific to that particular problem, and in all honesty, I could try for days to make the solution more general, and come out empty handed because I didn't understand the significance of the chunk sizes or why Uint8Array is used. A solution to the more general problem of reading a file line-by-line using a user-definable line separator (ideally with .split() since that also accept regex), and then doing something per-line (like printing it to the console.log) would be ideal.

like image 574
J.J Avatar asked Dec 08 '22 03:12

J.J


1 Answers

I've made a LineReader class at the following Gist URL. As I mentioned in a comment, it's unusual to use other line separators than LF, CR/LF and maybe CR. Thus, my code only considers LF and CR/LF as line separators.

https://gist.github.com/peteroupc/b79a42fffe07c2a87c28

Example:

new LineReader(file).readLines(function(line){
 console.log(line);
});
like image 141
Peter O. Avatar answered Apr 07 '23 15:04

Peter O.