I have a 1GB file that I would like to load into a javascript buffer and then read the first line of it. I wasn't sure what the correct 'type' to use for this would be. For example, here is the pseudocode I have:
file = new FileBuffer('/user/me/Desktop/file.csv')
first_line = file.getline(0)
What would actually be the proper way to do this in javascript (ArrayBuffer
or SharedArrayBuffer
or TypedArrayBuffer
?), and what is the suggested 'buffer-type' to use for a utf-8 file with a known size?
Uint8Array
is typically used for reading bytes of a file. Reading with a streams API will be much more performant than reading the entire file into memory, especially with large files or files sent over a network.
NodeJS: see Streams and fs.createReadStream()
. StringDecoder
could be used for converting bytes to strings.
Web: Fetch and Streams could be used with small responses from HTTP range requests. TextDecoder
could be used for converting bytes to strings.