Repo of the Day
krisk/Fuse
Published: Mar 1, 2023
Open repository ↗Lightweight fuzzy-search, in JavaScript
Summary
Fuse.js is a lightweight, zero-dependency fuzzy-search library written in TypeScript that runs in the browser and on Node.js. It uses the Bitap algorithm to match queries against small-to-medium datasets with typo tolerance, weighted fields, and operator-based extended search. The library ships in two builds: a basic fuzzy-only build (~6.8 kB min+gzip) and a full build with extended, logical, and token search (~8.6 kB min+gzip).
What it is useful for
Fuse.js targets client-side search of small-to-medium datasets where a dedicated search backend is not available or warranted. Common use cases include in-page search boxes, autocompletion in editors, filtering local JSON catalogs, and offline-capable search UIs.
For multi-word queries, the token search feature splits the input, fuzzy-matches each term independently, and ranks results with BM25-style IDF weighting so rare terms outweigh common ones. Per-field weighting lets a match in a document's title outrank the same match in its description. Extended search adds operators for exact match (=), prefix (^), suffix (!), and a structured object-query syntax with $eq, $contains, $startsWith, $endsWith, $and, $or, and $not. Object queries validate strictly: unknown operators, empty or non-string values, and illegal nesting throw instead of silently degrading to a fuzzy search.
For larger datasets, FuseWorker splits the index across Web Workers for parallel search; the README claims ~5x faster on 100K documents. Documented limitations: function-valued options (sortFn, getFn, keys[].getFn) cannot be used with FuseWorker because functions are not transferable, and Fuse.match() does not support useTokenSearch since token search requires corpus-level statistics.
How engineers can use it
Install via npm or yarn:
npm install fuse.js
# or
yarn add fuse.js
Or load it directly from a CDN:
<script src="https://cdn.jsdelivr.net/npm/fuse.js/dist/fuse.min.mjs"></script>
Basic fuzzy search:
import Fuse from 'fuse.js'
const books = [
{ title: "Old Man's War", author: 'John Scalzi' },
{ title: 'JavaScript: The Good Parts', author: 'Douglas Crockford' }
]
const fuse = new Fuse(books, { keys: ['title', 'author'] })
fuse.search('javscript')
For multi-word queries with per-word typo tolerance:
const fuse = new Fuse(docs, {
useTokenSearch: true,
keys: ['title', 'body']
})
fuse.search('express midleware rout')
The index supports live updates via fuse.add() and fuse.remove(). Character-level match indices for UI highlighting are available with includeMatches: true. The full API reference, configuration options, and scoring theory are hosted at fusejs.io and are not duplicated in the README, so refer there for details beyond the quick start.