JavaScript FileReader Slice Performance

Just for kicks, here it is with a worker thread and File System Access API

No idea if either of those things help, I have no 6gb files.
This will get the reading off the main thread so that does help performance in some sense.

Object.assign(
    new Worker(
        URL.createObjectURL(
            new Blob(
                [
                    `self.onmessage = async (e) =>` +
                    `    void postMessage(` +
                    `        (new FileReaderSync())` +
                    `            .readAsText(` +
                    `                (await e.data.getFile())` +
                    `                    .slice(0,1024*10)` +
                    `            )` +
                    `    );`
                ],
                { type: 'application/javascript' }
            )
        )
    ),
    { onmessage: (e) => void console.log(e.data) }
).postMessage(
    (await window.showOpenFilePicker(
        { mode: 'read', startIn: 'documents' }
    )).pop()
);

edit:

forgot but you need chromium for this to run sorry (tested on edge)
also this won’t run in a jsfiddle because web worker blah blah security blah blah. You can copy paste it into the console on google though. for some reason the headers don’t prevent thins from running. If this actually does help please actually put the worker in its own file (and reformat my artistic negative space triangle out of existence)

Leave a Comment

tech