Web Workers โ
FuseWorker distributes search across multiple Web Workers, searching dataset shards in parallel. The UI stays completely responsive โ no jank, no frozen frames โ even on 100K+ item datasets.
Try It โ
Click Search on each side and watch the balls. The FPS counter tells the story.
Dataset: randomly generated people with name, email, company, and description fields โ searched across all 4 keys.
When to Use It โ
The standard Fuse class searches synchronously on the main thread. For most datasets (hundreds to a few thousand items), this is fast enough. But with 10K+ items, search can take hundreds of milliseconds or more, visibly freezing the UI.
FuseWorker is designed for these cases:
- Large datasets โ 10K+ items where single-thread search causes noticeable lag
- Search-as-you-type โ where every keystroke triggers a search and frame drops are unacceptable
- Mobile devices โ where slower CPUs make the problem worse
If your dataset is small enough that Fuse feels instant, stick with Fuse. Simpler, synchronous, no cleanup needed.
Why not just use Web Workers yourself? โ
You can โ it's not too complicated. But FuseWorker handles the parts that are tedious to get right: splitting the dataset into shards, spawning and managing worker lifecycle, dispatching queries to all workers in parallel, and merging results back in sorted order. It's boilerplate you'd write every time, bundled into a drop-in class with the same API you already know.
Quick Start โ
import { FuseWorker } from 'fuse.js/worker'
const fuse = new FuseWorker(docs, {
keys: ['title', 'author', 'description'],
threshold: 0.4,
includeScore: true
})
const results = await fuse.search('javascript')
// Clean up when done
fuse.terminate()Almost the same API as Fuse, with a few caveats covered in Differences from Fuse below โ the headline one is that search() returns a Promise.
How It Works โ
- Lazy initialization โ Workers are not created until the first
search()call. - Data sharding โ The dataset is split evenly across N workers. Each worker creates its own
Fuseinstance with its shard. - Parallel search โ A search query is sent to all workers simultaneously. Each worker searches its shard independently.
- Merge โ Results from all workers are merged and sorted by score on the main thread.
Main Thread Worker Threads
โโโโโโโโโโโ โโโโโโโโโโโโโโ
FuseWorker.search('query')
โโโ postMessage โโโโโโโโโโโโโโโโโโโถ Worker 1: Fuse.search('query') on shard 1
โโโ postMessage โโโโโโโโโโโโโโโโโโโถ Worker 2: Fuse.search('query') on shard 2
โโโ postMessage โโโโโโโโโโโโโโโโโโโถ Worker 3: Fuse.search('query') on shard 3
โโโ postMessage โโโโโโโโโโโโโโโโโโโถ Worker 4: Fuse.search('query') on shard 4
โ
merge + sort โโโโโโโโโโโโโโโโโโโโโโโโโโAPI โ
Constructor โ
const fuse = new FuseWorker(docs, options?, workerOptions?)docsโ Array of documents to search (same asFuse)optionsโ Fuse.js options (same asFuse:keys,threshold,includeScore, etc.)workerOptionsโ Worker-specific options:
| Option | Type | Default | Description |
|---|---|---|---|
numWorkers | number | navigator.hardwareConcurrency (max 8) | Number of parallel workers |
workerUrl | string | URL | Auto-resolved | Custom path to the worker script |
search(query, options?) โ
const results = await fuse.search('query')
const limited = await fuse.search('query', { limit: 10 })Returns Promise<FuseResult[]>. Supports the same query types as Fuse: strings, and expression objects (logical search).
add(doc) โ
await fuse.add({ title: 'New Item', author: 'Jane' })Adds a document to one of the worker shards (round-robin distribution).
setCollection(docs) โ
await fuse.setCollection(newDocs)Replaces the entire dataset. Redistributes across workers and rebuilds indexes. Use this instead of remove() โ filter your array, then call setCollection.
terminate() โ
fuse.terminate()Terminates all workers and cleans up resources. Always call this when you're done searching.
Performance โ
Benchmarked in Chrome on 100,000 documents with 4 searchable keys:
| Method | Time | Speedup |
|---|---|---|
| Fuse (single thread) | 6,276ms | 1.00x |
| FuseWorker (2 workers) | 3,514ms | 1.79x |
| FuseWorker (4 workers) | 1,909ms | 3.29x |
| FuseWorker (8 workers) | 1,338ms | 4.69x |
Scaling is near-linear. The sweet spot is typically 4 workers โ beyond that, the marginal gains diminish and memory usage increases (each worker holds a copy of its shard).
Want to run more detailed benchmarks? See bench/parallel-browser in the repo.
Overhead โ
Workers add overhead from data serialization (postMessage) and worker startup. For small datasets, this overhead outweighs the parallelism benefit. As a rule of thumb:
- < 5K items โ Use
Fuse. Workers add latency. - 5Kโ10K items โ Either works. Test with your data.
- 10K+ items โ
FuseWorkeris faster and keeps the UI responsive.
Differences from Fuse โ
| Fuse | FuseWorker | |
|---|---|---|
search() return type | FuseResult[] | Promise<FuseResult[]> |
| UI blocking | Yes | No |
remove(predicate) | Supported | Use setCollection() instead |
getIndex() | Supported | Not available |
| Result object references | Same as input docs | Copies (structured clone) |
Custom sortFn | Supported | Throws at construction |
Custom getFn (top-level) | Supported | Throws at construction |
Custom keys[].getFn | Supported | Throws at construction |
| Cleanup required | No | Call terminate() |
Unsupported options โ
FuseWorker rejects function-valued options at construction time. Functions can't be transferred to a worker via postMessage (they aren't structured-cloneable), so FuseWorker throws an explicit error instead of failing later with an opaque DataCloneError.
The unsupported options are:
sortFnโFuseWorkeralways sorts results by Fuse's default(score, refIndex)tie-break. If you need a custom sort, runFuseon the main thread or sort the returned array yourself.getFn(top-level) โ Fall back to dotted key paths ('a.b.c') or array paths (['a', 'b', 'c']).keys[].getFnโ Same as above. Use a string or array path on the key.useTokenSearchโ Token search depends on corpus-level statistics (df,fieldCount) that would be computed independently inside each worker shard, producing scores that don't match single-threadFuse. UseFusedirectly on the main thread for token search.
Default ordering is preserved: FuseWorker returns the same order as Fuse for the same inputs (with or without includeScore), and shouldSort: false returns results in global collection order.
Worker URL โ
FuseWorker needs to load a separate script file inside each Web Worker. By default, it finds this file automatically โ you don't need to do anything.
Some bundlers (e.g., certain Webpack configurations) can't resolve the worker script path automatically. If you see errors about the worker failing to load, you can provide the path explicitly:
import workerUrl from 'fuse.js/worker-script'
const fuse = new FuseWorker(docs, options, { workerUrl })If that doesn't work with your bundler either, copy dist/fuse.worker.mjs to your public/static directory and pass the URL directly:
const fuse = new FuseWorker(docs, options, {
workerUrl: '/static/fuse.worker.mjs'
})Browser Support โ
Web Workers are supported in all modern browsers (Chrome, Firefox, Safari, Edge). FuseWorker will not work in:
- Node.js โ
FuseWorkeruses the browser's Web Worker API, not Node'sworker_threads. UseFuseon the server. - Restrictive CSP policies โ some Content Security Policies block
new Worker(). Check your CSP headers if workers fail to spawn.

