fuse.js vs fuzzy-search vs fuzzysort
クライアントサイドにおけるファジー検索ライブラリの選定
fuse.jsfuzzy-searchfuzzysort類似パッケージ:

クライアントサイドにおけるファジー検索ライブラリの選定

fuse.js、fuzzy-search、fuzzysort は、すべて JavaScript 環境で動作するファジー検索(あいまい検索)ライブラリです。ユーザーの入力ミスや部分的な一致を許容してリストからアイテムを検索する機能を提供しますが、内部アルゴリズム、パフォーマンス特性、設定の柔軟性に違いがあります。fuse.js は設定項目が豊富で柔軟性が高く、fuzzy-search はシンプルで導入が容易、fuzzysort は速度とインデックス機能に優れています。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
fuse.js020,454417 kB11ヶ月前Apache-2.0
fuzzy-search0229-167年前ISC
fuzzysort04,34478.9 kB710日前MIT

クライアントサイド検索の決定打:fuse.js vs fuzzy-search vs 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 つのライブラリには、ファジー検索ツールとしての共通する基盤があります。

1. 📦 データ構造の扱い

  • すべて 配列(Array) 内のオブジェクトを検索対象とします。
  • 特定のキー(プロパティ)を指定して検索範囲を絞れます。
// 全ライブラリ共通のコンセプト
const data = [{ title: 'Apple' }, { title: 'Banana' }];
// 'title' キーを対象に検索を実行

2. 🔤 文字列マッチング

  • 完全一致ではなく、部分一致 や 順序の入れ替わり を許容します。
  • ユーザーの入力ミスを吸収する目的で利用されます。
// 例:"aple" と入力しても "Apple" にマッチ
// 各ライブラリともこの挙動をサポート

3. 🌐 クライアントサイド実行

  • サーバーを介さず、ブラウザ上 で検索を完結させます。
  • API リクエストのオーバーヘッドがなく、即時に結果が表示されます。
// 全ライブラリとも npm からインストールし、フロントエンドで動作
import Fuse from 'fuse.js';
import FuzzySearch from 'fuzzy-search';
import fuzzysort from 'fuzzysort';

4. ✅ 結果のフィルタリング

  • 検索パターンに一致しないアイテムは結果から除外されます。
  • 空の配列または結果オブジェクトを返します。
// 一致するものがない場合
const results = searcher.search('xyz'); 
// -> [] または 空の結果

5. 🛠️ 開発者体験

  • TypeScript の型定義が利用可能(またはコミュニティ製)です。
  • npm エコシステムの一部として、モダンなビルドツールと連携できます。
// TypeScript での利用例(共通)
// 型安全性を確保しながら検索ロジックを実装可能

📊 比較サマリー

特徴fuse.jsfuzzy-searchfuzzysort
主な目的柔軟性と精度シンプルさ速度
設定項目非常に多い少ない中程度
インデックス対応(オプション)非対応強力なサポート
一致情報詳細なインデックスインデックスインデックス
メンテナンス活発安定活発

💡 結論:どのライブラリを選ぶべきか

fuse.js は、機能の豊富さとドキュメントの充実度において業界標準と言えます。検索の挙動を細かく制御したい場合や、将来の要件変更に備えたい場合は、これが safest choice です。少し設定が複雑ですが、その分できることが多いです。

fuzzy-search は、とにかく早く実装を終わらせたい場合や、検索ロジックが単純で済む場合に有効です。ただし、データ量が増えた際のパフォーマンスには注意を払う必要があります。

fuzzysort は、パフォーマンスがボトルネックになることが明確な場合に選択します。数万件以上のデータをクライアント側で処理する必要があるなら、インデックス機能を持つこれ一択でしょう。

最終的なアドバイス: 迷ったら fuse.js から始めましょう。コミュニティが大きく、困ったときに情報が見つかりやすいからです。ただし、プロファイリングの結果、検索処理が重いことが判明した場合は、fuzzysort への移行を検討する価値があります。

選び方: fuse.js vs fuzzy-search vs fuzzysort

  • fuse.js:

    高度な検索設定(重み付け、しきい値調整、位置情報など)が必要な場合に選択します。大規模なコミュニティと豊富なドキュメントがあるため、複雑な検索要件を持つプロジェクトや、長期的なメンテナンスを重視する場合に適しています。

  • fuzzy-search:

    設定を最小限に抑え、すぐに実装したい場合に選択します。機能よりもシンプルさを優先する小規模なプロジェクトや、プロトタイプ作成時に適していますが、大規模データでのパフォーマンス制限に注意が必要です。

  • fuzzysort:

    検索速度が最優先事項である場合に選択します。大量のデータを扱う場合や、事前インデックス作成機能を活用してパフォーマンスを最適化したい場合に適しています。アルゴリズムのカスタマイズ性は fuse.js に劣ります。

fuse.js のREADME

Fuse.js

Node.js CI Version Downloads code style: prettier Contributors License

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.

✨ What's New: Token Search

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.

Web Workers

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.

Installation

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>

Quick Start

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', ... }, ... }]

Features

Fuzzy Search

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' } }]

Weighted Keys

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 }
  ]
})

Extended Search

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

Token Search

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
  • Typo tolerance per word — each term is fuzzy-matched independently
  • Relevance ranking — rare terms are weighted higher than common ones
  • Word order independent — "patterns javascript" and "javascript patterns" return identical results
  • No query length limit — long multi-word queries work naturally since each term is searched separately
  • AND or OR — tokenMatch: 'all' returns only records matching every word (filtering); the default 'any' matches any word
  • Custom tokenizer — pass a regex or function via tokenize for tokens with internal punctuation (node.js, c++), or use Intl.Segmenter for CJK / Thai word segmentation. Unicode-aware by default

Available in the full build. See the Token Search docs for details and performance benchmarks.

Logical Search

Combine conditions with $and and $or for complex queries. Available in the full build.

fuse.search({
  $and: [
    { title: 'javascript' },
    { author: 'crockford' }
  ]
})

Match Highlighting

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]]

Single String Matching

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.

Dynamic Collections

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')

Builds

Fuse.js ships in two variants:

BuildIncludesMin + gzip
FullFuzzy + Extended + Logical + Token search~8.6 kB
BasicFuzzy search only~6.8 kB

Use the basic build if you only need fuzzy search and want the smallest bundle size.

Documentation

For the full API reference, configuration options, scoring theory, and interactive demos, visit fusejs.io.

Official ports

  • fuse-swift: Swift port for iOS, macOS, tvOS, watchOS, visionOS, and Linux. Byte-equivalent results, idiomatic Swift API, syncs with each upstream release. Currently in 2.0.0-rc.1.

Supporting Fuse.js

Develop

See DEVELOPERS.md for setup, scripts, and project structure.

Contribute

See CONTRIBUTING.md for guidelines on issues and pull requests.