Fast Fuzzy Search Utility

1.12.0 · active · verified Sun Apr 19

fast-fuzzy is a compact and high-performance JavaScript utility for fuzzy string matching, currently at version 1.12.0. It implements a modified Levenshtein distance algorithm, specifically Damerau-Levenshtein distance, which is more forgiving of transpositions. The library preprocesses inputs through UTF-8 normalization, optional lowercasing, symbol stripping, and whitespace normalization to ensure robust matching. It scores matches between 0 and 1, returning results sorted by score, then by match earliness, and finally by length proximity to the search term. For efficiency, especially when searching the same set of candidates repeatedly, it internally uses a trie data structure to cache work and prune non-matching subtrees, significantly outperforming brute-force approaches. While it offers a simple `search` function for one-off queries, the `Searcher` class is recommended for persistent collections due to its trie caching. The project appears to have a steady, though not strictly scheduled, release cadence, with ongoing maintenance.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing a `Searcher` with complex objects and a `keySelector`, performing a fuzzy search, and dynamically adding new candidates, with results filtered by a threshold.

import { Searcher } from 'fast-fuzzy';

interface Product {
  id: string;
  name: string;
  description: string;
  tags: string[];
}

const products: Product[] = [
  { id: '1', name: 'Apple MacBook Pro', description: 'High-performance laptop', tags: ['laptop', 'apple'] },
  { id: '2', name: 'Google Pixel 8 Pro', description: 'Advanced smartphone', tags: ['phone', 'android'] },
  { id: '3', name: 'Microsoft Surface Laptop', description: 'Versatile notebook', tags: ['laptop', 'microsoft'] },
  { id: '4', name: 'Apple Watch Series 9', description: 'Smartwatch with health features', tags: ['wearable', 'apple'] }
];

// Initialize Searcher with candidates and a keySelector for object properties
const productSearcher = new Searcher(products, {
  keySelector: (obj: Product) => [obj.name, obj.description, ...obj.tags],
  threshold: 0.7 // Only show results with a score of 0.7 or higher
});

// Perform a search
const results = productSearcher.search('apl watch');

console.log('Search results for "apl watch":');
results.forEach(result => {
  console.log(`- ${result.item.name} (Score: ${result.score.toFixed(2)})`);
});

// Add a new product dynamically
productSearcher.add({ id: '5', name: 'Samsung Galaxy Book', description: 'Lightweight laptop', tags: ['laptop', 'samsung'] });

const newResults = productSearcher.search('samsg book');
console.log('\nSearch results for "samsg book" after adding a new product:');
newResults.forEach(result => {
  console.log(`- ${result.item.name} (Score: ${result.score.toFixed(2)})`);
});

view raw JSON →