fuse.js、fuzzy-search、fuzzysort は、すべて JavaScript 環境で動作するファジー検索(あいまい検索)ライブラリです。ユーザーの入力ミスや部分的な一致を許容してリストからアイテムを検索する機能を提供しますが、内部アルゴリズム、パフォーマンス特性、設定の柔軟性に違いがあります。fuse.js は設定項目が豊富で柔軟性が高く、fuzzy-search はシンプルで導入が容易、fuzzysort は速度とインデックス機能に優れています。
フロントエンドアプリケーションで検索機能を実装する際、完全一致ではなく「あいまい検索(ファジー検索)」が必要になる場面は多岐にわたります。ユーザーのタイポ対策、部分一致、あるいは順序が異なるキーワードの検索などです。fuse.js、fuzzy-search、fuzzysort はいずれもこの課題を解決する人気ライブラリですが、設計思想と得意分野が明確に異なります。
エンジニアとして、単に「動くもの」を選ぶのではなく、データ規模、検索の複雑さ、そしてパフォーマンス要件に基づいて選定する必要があります。ここでは、実務的な観点からこれら 3 つのライブラリを比較します。
検索の「賢さ」は、ライブラリがどのように文字の一致を判定するかによります。
fuse.js は、ビットアルゴリズムに基づいた高度なスコアリングシステムを採用しています。文字の位置や近接度を考慮し、一致度のスコアを細かく計算できます。
// fuse.js: 詳細なスコアリング
const fuse = new Fuse(items, {
keys: ['title'],
threshold: 0.3, // 一致度のしきい値
location: 0,
distance: 100
});
const results = fuse.search('iphone');
// results: [{ item: {...}, score: 0.15, ... }]
fuzzy-search は、より単純なアルゴリズムを使用します。設定が少なく、直感的ですが、複雑なパターンマッチングには限界があります。
// fuzzy-search: シンプルな実装
const searcher = new FuzzySearch(items, ['title'], {
caseSensitive: false
});
const results = searcher.search('iphone');
// results: [{ ...matched item... }]
fuzzysort は、速度を重視したアルゴリズムを採用しており、C++ などの実装を参考にした効率的なマッチングを行います。精度よりもスピードを優先する設計です。
// fuzzysort: 速度重視
const results = fuzzysort.go('iphone', items, {
key: 'title',
limit: 10
});
// results: [{ score: -10, obj: {...}, ... }]
プロジェクトが大きくなるほど、検索挙動の微調整が必要になります。
fuse.js は、設定オプションが非常に豊富です。キーごとの重み付け、トークン化、タグ検索など、細かな制御が可能です。
// fuse.js: 高度な設定
const fuse = new Fuse(items, {
keys: [
{ name: 'title', weight: 0.7 },
{ name: 'tags', weight: 0.3 }
],
includeScore: true,
includeMatches: true
});
fuzzy-search は、設定項目が限られています。大文字小文字の区別や、ソート機能など基本的なもののみです。
// fuzzy-search: 最小限の設定
const searcher = new FuzzySearch(items, ['title'], {
sort: true,
caseSensitive: false
});
fuzzysort は、パフォーマンスに関わる設定に焦点を当てています。アルゴリズムの挙動そのものよりも、どのようにデータを処理するかに重点を置きます。
// fuzzysort: パフォーマンス設定
const results = fuzzysort.go('query', items, {
key: 'title',
allowTypo: true // タイポ許容
});
データ量が数千件を超えると、パフォーマンスの違いが顕著になります。
fuse.js は、検索時にリストを走査します。データ量が増えると検索時間が長くなる傾向がありますが、最近のバージョンではインデックス機能も強化されています。
// fuse.js: 検索実行
// 毎回リストを評価するため、大量データでは注意が必要
const results = fuse.search(query);
fuzzy-search は、小〜中規模のデータセット向けに設計されています。大規模データでの最適化機能は限定的です。
// fuzzy-search: 検索実行
// シンプルな実装のため、データ量に比例して負荷が増加
const results = searcher.search(query);
fuzzysort の最大の強みは「準備(prepare)」機能です。データを事前にインデックス化することで、検索速度を劇的に向上させられます。
// fuzzysort: 事前準備と検索
const preparedItems = items.map(item => fuzzysort.prepare(item.title));
// 検索時に準備済みデータを使用することで高速化
const results = fuzzysort.go('query', preparedItems);
検索結果を UI に表示する際、どの部分が一致したかをハイライト表示したい場合があります。
fuse.js は、includeMatches: true を設定することで、一致したインデックス情報を結果に含めます。
// fuse.js: 一致箇所の取得
const fuse = new Fuse(items, { includeMatches: true, keys: ['title'] });
const results = fuse.search('app');
// results[0].matches[0].indices -> [[0, 2]] ("app" の位置)
fuzzy-search も同様に、一致したインデックスを結果オブジェクトに含めます。
// fuzzy-search: 一致箇所の取得
const searcher = new FuzzySearch(items, ['title']);
const results = searcher.search('app');
// results[0].matchIndices -> [0, 1, 2]
fuzzysort は、結果オブジェクトにインデックス情報を含みます。これを使って UI 上でハイライト処理を行います。
// fuzzysort: 一致箇所の取得
const results = fuzzysort.go('app', items, { key: 'title' });
// results[0].indexes -> [0, 1, 2]
これら 3 つのライブラリには、ファジー検索ツールとしての共通する基盤があります。
// 全ライブラリ共通のコンセプト
const data = [{ title: 'Apple' }, { title: 'Banana' }];
// 'title' キーを対象に検索を実行
// 例:"aple" と入力しても "Apple" にマッチ
// 各ライブラリともこの挙動をサポート
// 全ライブラリとも npm からインストールし、フロントエンドで動作
import Fuse from 'fuse.js';
import FuzzySearch from 'fuzzy-search';
import fuzzysort from 'fuzzysort';
// 一致するものがない場合
const results = searcher.search('xyz');
// -> [] または 空の結果
// TypeScript での利用例(共通)
// 型安全性を確保しながら検索ロジックを実装可能
| 特徴 | fuse.js | fuzzy-search | fuzzysort |
|---|---|---|---|
| 主な目的 | 柔軟性と精度 | シンプルさ | 速度 |
| 設定項目 | 非常に多い | 少ない | 中程度 |
| インデックス | 対応(オプション) | 非対応 | 強力なサポート |
| 一致情報 | 詳細なインデックス | インデックス | インデックス |
| メンテナンス | 活発 | 安定 | 活発 |
fuse.js は、機能の豊富さとドキュメントの充実度において業界標準と言えます。検索の挙動を細かく制御したい場合や、将来の要件変更に備えたい場合は、これが safest choice です。少し設定が複雑ですが、その分できることが多いです。
fuzzy-search は、とにかく早く実装を終わらせたい場合や、検索ロジックが単純で済む場合に有効です。ただし、データ量が増えた際のパフォーマンスには注意を払う必要があります。
fuzzysort は、パフォーマンスがボトルネックになることが明確な場合に選択します。数万件以上のデータをクライアント側で処理する必要があるなら、インデックス機能を持つこれ一択でしょう。
最終的なアドバイス: 迷ったら fuse.js から始めましょう。コミュニティが大きく、困ったときに情報が見つかりやすいからです。ただし、プロファイリングの結果、検索処理が重いことが判明した場合は、fuzzysort への移行を検討する価値があります。
高度な検索設定(重み付け、しきい値調整、位置情報など)が必要な場合に選択します。大規模なコミュニティと豊富なドキュメントがあるため、複雑な検索要件を持つプロジェクトや、長期的なメンテナンスを重視する場合に適しています。
設定を最小限に抑え、すぐに実装したい場合に選択します。機能よりもシンプルさを優先する小規模なプロジェクトや、プロトタイプ作成時に適していますが、大規模データでのパフォーマンス制限に注意が必要です。
検索速度が最優先事項である場合に選択します。大量のデータを扱う場合や、事前インデックス作成機能を活用してパフォーマンスを最適化したい場合に適しています。アルゴリズムのカスタマイズ性は fuse.js に劣ります。
Fuse.js is a lightweight, zero-dependency fuzzy-search library written in TypeScript. It works in the browser and on the server, and is designed for searching small-to-medium datasets on the client side where you can't rely on a dedicated search backend.
Multi-word fuzzy search with relevance ranking. Type "javascrpt paterns" and find "JavaScript Patterns" — typo tolerance, multiple words, and smart ranking all at once.
const fuse = new Fuse(docs, {
useTokenSearch: true,
keys: ['title', 'author', 'description']
})
fuse.search('javascrpt paterns')
// → [{ item: { title: 'JavaScript Patterns', ... } }]
See Token Search below for details.
Search large datasets without freezing the UI. FuseWorker splits your data across multiple Web Workers and searches in parallel — ~5x faster on 100K documents.
import { FuseWorker } from 'fuse.js/worker'
const fuse = new FuseWorker(docs, {
keys: ['title', 'author', 'description']
})
const results = await fuse.search('query')
fuse.terminate()
Same options and results as Fuse — just async. Function-valued options (sortFn, getFn, keys[].getFn) aren't supported because functions can't be transferred to a worker; everything else carries over. See the Web Workers docs for the interactive demo and full API.
npm install fuse.js
yarn add fuse.js
Or include directly via CDN:
<script src="https://cdn.jsdelivr.net/npm/fuse.js/dist/fuse.min.mjs"></script>
import Fuse from 'fuse.js'
const books = [
{ title: "Old Man's War", author: 'John Scalzi' },
{ title: 'The Lock Artist', author: 'Steve Hamilton' },
{ title: 'HTML5', author: 'Remy Sharp' },
{ title: 'JavaScript: The Good Parts', author: 'Douglas Crockford' }
]
const fuse = new Fuse(books, {
keys: ['title', 'author']
})
fuse.search('javscript')
// → [{ item: { title: 'JavaScript: The Good Parts', ... }, ... }]
The core of Fuse.js. Uses the Bitap algorithm for approximate string matching — handles typos, misspellings, and partial matches out of the box.
fuse.search('javscript')
// → [{ item: { title: 'JavaScript: The Good Parts', author: 'Douglas Crockford' } }]
Search across multiple fields with different importance levels. Title matches can rank higher than description matches.
const fuse = new Fuse(docs, {
keys: [
{ name: 'title', weight: 2 },
{ name: 'description', weight: 1 }
]
})
Use operators for precise control: exact match (=), prefix (^), suffix (!), and more. Enable with useExtendedSearch: true.
const fuse = new Fuse(list, {
useExtendedSearch: true,
keys: ['title']
})
fuse.search('=exact match') // exact match
fuse.search('^prefix') // starts with
fuse.search('!term') // does not include
Splits multi-word queries into individual terms, fuzzy-matches each independently, and ranks results using BM25-style IDF weighting. Enable with useTokenSearch: true.
const fuse = new Fuse(docs, {
useTokenSearch: true,
keys: ['title', 'body']
})
fuse.search('express midleware rout')
// Finds "Express Middleware" and "Express Routing Guide" despite typos
"patterns javascript" and "javascript patterns" return identical resultstokenMatch: 'all' returns only records matching every word (filtering); the default 'any' matches any wordtokenize for tokens with internal punctuation (node.js, c++), or use Intl.Segmenter for CJK / Thai word segmentation. Unicode-aware by defaultAvailable in the full build. See the Token Search docs for details and performance benchmarks.
Combine conditions with $and and $or for complex queries. Available in the full build.
fuse.search({
$and: [
{ title: 'javascript' },
{ author: 'crockford' }
]
})
Get character-level match indices for highlighting search results in your UI.
const fuse = new Fuse(list, {
includeMatches: true,
keys: ['title']
})
const result = fuse.search('javscript')
// result[0].matches[0].indices → [[0, 9]]
Use Fuse.match() to fuzzy-match a pattern against a single string without creating an index. Useful for one-off comparisons or custom filtering.
const result = Fuse.match('javscript', 'JavaScript: The Good Parts')
// → { isMatch: true, score: 0.04, indices: [[0, 9]] }
Fuse.match() does not support useTokenSearch — token search requires corpus-level statistics (df, fieldCount) that a one-off string comparison can't provide. Passing useTokenSearch: true throws an explicit error. Use new Fuse(docs, { useTokenSearch: true }).search(query) for token-search behavior.
Add and remove documents from a live index without rebuilding.
fuse.add({ title: 'New Book', author: 'New Author' })
fuse.remove((doc) => doc.title === 'Old Book')
Fuse.js ships in two variants:
| Build | Includes | Min + gzip |
|---|---|---|
| Full | Fuzzy + Extended + Logical + Token search | ~8.6 kB |
| Basic | Fuzzy search only | ~6.8 kB |
Use the basic build if you only need fuzzy search and want the smallest bundle size.
For the full API reference, configuration options, scoring theory, and interactive demos, visit fusejs.io.
See DEVELOPERS.md for setup, scripts, and project structure.
See CONTRIBUTING.md for guidelines on issues and pull requests.