JavaScript read large file line by line

约 2 分钟 #JavaScript
杨秋逸
杨秋逸

MDN Docs

This example shows how you might fetch a text file and handle it as a stream of text lines. It deals with stream chunks not ending on line boundaries, and with converting from Uint8Array to strings.

async function* makeTextFileLineIterator(fileURL) {
    const utf8Decoder = new TextDecoder('utf-8')
    let response = await fetch(fileURL)
    let reader = response.body.getReader()
    let { value: chunk, done: readerDone } = await reader.read()
    chunk = chunk ? utf8Decoder.decode(chunk, { stream: true }) : ''

    let re = /\r\n|\n|\r/gm
    let startIndex = 0

    for (;;) {
        let result = re.exec(chunk)
        if (!result) {
            if (readerDone) {
                break
            }
            let remainder = chunk.substr(startIndex)
            ;({ value: chunk, done: readerDone } = await reader.read())
            chunk =
                remainder + (chunk ? utf8Decoder.decode(chunk, { stream: true }) : '')
            startIndex = re.lastIndex = 0
            continue
        }
        yield chunk.substring(startIndex, result.index)
        startIndex = re.lastIndex
    }
    if (startIndex < chunk.length) {
        // last line didn't end in a newline char
        yield chunk.substr(startIndex)
    }
}

for await (let line of makeTextFileLineIterator(urlOfFile)) {
    processLine(line)
}