Skip to main content

Web Workers

Parsing and writing large spreadsheets takes time. During the process, if the SheetJS library is running in the web browser, the website may freeze.

Workers provide a way to off-load the hard work so that the website does not freeze during processing.

Browser Compatibility

IE10+ and modern browsers support basic Web Workers. Some APIs like fetch were added later. Feature testing is highly recommended.

Inline Workers

Due to limitations of the live codeblocks, all of the workers in this section are in-line. The code is embedded in template literals. For production sites, typically workers are written in separate JS files.

Example (click to show)

For example, an in-line worker like

  const worker = new Worker(URL.createObjectURL(new Blob([`\
/* load standalone script from CDN */
importScripts("https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js");

/* this callback will run once the main context sends a message */
self.addEventListener('message', (e) => {
/* Pass the version string back */
postMessage({ version: XLSX.version });
}, false);
`])));

would typically be stored in a separate JS file like "worker.js":

worker.js
/* load standalone script from CDN */
importScripts("https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js");

/* this callback will run once the main context sends a message */
self.addEventListener('message', (e) => {
/* Pass the version string back */
postMessage({ version: XLSX.version });
}, false);

and the main script would pass a URL:

  const worker = new Worker("./worker.js");

Installation

In all cases, importScripts in a Worker can load the Standalone scripts

importScripts("https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js");

For production use, it is highly encouraged to download and host the script.

ECMAScript Module Support (click to show)
Browser Compatibility

ESM is supported in Web Workers in the Chromium family of browsers (including Chrome and Edge) as well as in browsers powered by WebKit (including Safari).

For support in legacy browsers like Firefox, importScripts should be used.

import * as XLSX from "https://cdn.sheetjs.com/xlsx-latest/package/xlsx.mjs";

When using modules, the script must be served with the correct MIME type and the Worker constructor must set the type option:

const worker_code = `\
/* load standalone script from CDN */
import * as XLSX from "https://cdn.sheetjs.com/xlsx-latest/package/xlsx.mjs";
// ... do something with XLSX here ...
`;
const worker = new Worker(
URL.createObjectURL(
new Blob(
[ worker_code ],
{ type: "text/javascript" } // second argument to the Blob constructor
)
),
{type: "module"} // second argument to Worker constructor
);

Downloading a Remote File

fetch in Web Workers

fetch was enabled in Web Workers in Chrome 42 and Safari 10.3

Typically the Web Worker performs the fetch operation, processes the workbook, and sends a final result to the main browser context for processing.

In the following example, the script:

  • downloads https://sheetjs.com/pres.numbers in a Web Worker
  • loads the SheetJS library and parses the file in the Worker
  • generates an HTML string of the first table in the Worker
  • sends the string to the main browser context
  • adds the HTML to the page in the main browser context
Result
Loading...
Live Editor

Creating a Local File

Writing files from Web Workers

XLSX.writeFile will not work in Web Workers! Raw file data can be passed from the Web Worker to the main browser context for downloading.

In the following example, the script:

  • generates a workbook object in the Web Worker
  • generates a XLSB file using XLSX.write in the Web Worker
  • generates an object URL in the Web Worker
  • sends the object URL to the main browser context
  • performs a download action in the main browser context
Result
Loading...
Live Editor

User-Submitted File

FileReaderSync

Typically FileReader is used in the main browser context. In Web Workers, the synchronous version FileReaderSync is more efficient.

In the following example, when a file is dropped over the DIV or when the INPUT element is used to select a file, the script:

  • sends the File object to the Web Worker
  • loads the SheetJS library and parses the file in the Worker
  • generates an HTML string of the first table in the Worker
  • sends the string to the main browser context
  • adds the HTML to the page in the main browser context
Result
Loading...
Live Editor

Streaming Write

A more general discussion, including row-oriented processing demos, is included in the "Large Datasets" demo.

File System Access API

Browser Compatibility

At the time of writing, the File System Access API is only available in Chromium and Chromium-based browsers like Chrome and Edge.

Performance

In local testing, committing each CSV row as it is generated is significantly slower than accumulating and writing once at the end.

When the target CSV is known to be less than 500MB, it is preferable to batch. Strings larger than 500M may hit browser length limits.

The following live demo fetches and parses a file in a Web Worker. The script:

  • prompts user to save file (window.showSaveFilePicker in the main thread)
  • passes the URL and the file object to the Web Worker
  • loads the SheetJS library in the Web Worker
  • fetches the requested URL and parses the workbook from the Worker
  • creates a Writable Stream from the file object.
  • uses XLSX.stream.to_csv to generate CSV rows of the first worksheet
    • every 100th row, a progress message is sent back to the main thread
    • at the end, a completion message is sent back to the main thread

The demo has a checkbox. If it is not checked (default), the Worker will collect each CSV row and write once at the end. If it is checked, the Worker will try to commit each row as it is generated.

The demo also has a URL input box. Feel free to change the URL. For example:

https://raw.githubusercontent.com/SheetJS/test_files/master/large_strings.xls is an XLS file over 50 MB. The generated CSV file is about 55 MB.

https://raw.githubusercontent.com/SheetJS/libreoffice_test-files/master/calc/xlsx-import/perf/8-by-300000-cells.xlsx is an XLSX file with 300000 rows (approximately 20 MB) yielding a CSV of 10 MB.

Result
Loading...
Live Editor