Installations
npm install @nozbe/microfuzz
Score
89.2
Supply Chain
100
Quality
81.9
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
16.19.0
NPM Version
8.19.3
Statistics
139 Stars
31 Commits
3 Forks
4 Watching
2 Branches
3 Contributors
Updated on 27 Nov 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
395,705
Last day
-23.7%
1,386
Compared to previous day
Last week
-7%
8,146
Compared to previous week
Last month
4.7%
32,074
Compared to previous month
Last year
1,807.6%
375,995
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
27
microfuzz
A tiny, simple, fast JS fuzzy search library
✨ Easily add power user-friendly search, autocomplete, jump to, command palette to your app.
microfuzz | |
---|---|
🤓 | Fuzzy search. Power users love it |
🗜️ | Tiny. 2KB gzipped |
✅ | Simple. Only a few options, reasonable defaults |
⚡️ | Fast. Filter thousands of items in milliseconds |
🧰 | Framework-agnostic. Plain JS, no dependencies |
⚛️ | React/React Native helpers (optional) included |
⚠️ | Static typing with Flow or TypeScript |
microfuzz pitch
General idea of how microfuzz
works:
- Case-insensitive and diacritics-insensitive search
- Works with Latin script, Cyrillic, rudimentary CJK support
- Limited fuzzing: matches query letters in order, but they don't have to be consecutive (but transposition and missing characters are not allowed)
- Some very poor fuzzy matches are rejected by default (see Fuzzy search strategies)
- Additionally, matches query words in any order
- NOT full-text search. Stemming, soundex, levenstein, autocorrect are not included
- Sorts by how well text matches the query with simple heuristics (for equal fuzzy score, input order is preserved, so you can pre-sort array if you want).
- Returns ranges of matching characters for pretty highlighting
- In-memory search, no indexing
microfuzz
is not a one-size-fits-all solution (see Alternatives to consider).
Demo
Using microfuzz (plain JS)
1import createFuzzySearch from '@nozbe/microfuzz' 2 3const list = [/* an array of strings to fuzzy search */] 4const fuzzySearch = createFuzzySearch(list) 5 6// Run this whenever search term changes 7// Only matching items will be returned, sorted by how well they match `queryText` 8const results = fuzzySearch(queryText)
This is split into two steps for performance (createFuzzySearch
pre-processes list
, and you can cache/memoize function returned by it).
If list
is an array of objects:
1const fuzzySearch = createFuzzySearch(list, {
2 // search by `name` property
3 key: 'name',
4 // search by `description.text` property
5 getText: (item) => [item.description.text]
6 // search by multiple properties:
7 getText: (item) => [item.name, item.description.text]
8})
Using microfuzz in React
If you use React or React Native, you can use these optional helpers for convenience:
1import { useFuzzySearchList, Highlight } from '@nozbe/microfuzz/react' 2 3// `useFuzzySearchList` simply wraps `createFuzzySearch` with memoization built in 4// NOTE: For best performance, `getText` and `mapResultItem` should be memoized by user 5const filteredList = useFuzzySearchList({ 6 list, 7 // If `queryText` is blank, `list` is returned in whole 8 queryText, 9 // optional `getText` or `key`, same as with `createFuzzySearch` 10 getText: (item) => [item.name], 11 // arbitrary mapping function, takes `FuzzyResult<T>` as input 12 mapResultItem: ({ item, score, matches: [highlightRanges] }) => ({ item, highlightRanges }) 13}) 14 15// Render `filteredList`'s labels with matching characters highlighted 16filteredList.map(({ item, highlightRanges }) => ( 17 <Item key={item.key}> 18 <Label><Highlight text={item.name} ranges={highlightRanges} /></Label> 19 </Item> 20))
Fuzzy search strategies
You can optionally pass { strategy: }
parameter to createFuzzySearch
/ useFuzzySearchList
:
'off'
- no fuzzy search, only matches if item contains query (or contains query words in any order)'smart'
- (default) matches letters in order, but poor quality matches are ignored'aggressive'
- matches letters in order with no restrictions (classic fuzzy search)
Alternatives to consider
I wrote microfuzz
simply because I didn't quite like how other fuzzy search libraries I found worked, for my use case. Your mileage may vary.
It's not the tiniest, the simplest, or the fastest implementation you can find. But it's tiny, simple, and fast enough, while providing fuzzy search heuristics and sorting that I found to work reasonably well in Nozbe, a project management app, where it's used to filter down or autocomplete lists of short labels — names of projects, sections, tasks, user names, etc.
By "fast" I mean that on my computer, with a list of ~4500 labels, the first search (one-letter search query) takes ~7ms, while subsequent searches take less than 1.5ms — all in-memory, without indexing. More than fast enough to search on every keystroke without any lag.
If you have much larger lists to fuzzy-search, you may find the performance unsatisfactory — consider implementations with simpler heuristics or indexing. For very long strings (notes, comments), fuzzy-searching may not be the right strategy — consider Full-Text Search (with indexing) instead.
Feel free to contribute improvements to sorting heuristics or alternative search strategies (provided that the "fast, simple, tiny" criteria don't suffer too much).
Alternatives:
- Fuse.js - popular implementation with many more options, including extended search and indexing. However, while its scoring (sorting) is much more sophisticated in theory, I found it unsatisfactory in practice.
- fuzzysort - faster and really good for fuzzy searching lists of file names/file paths, but I don't like its scoring for natural language labels. I borrowed the test data from fuzzysort so you can compare both demos side by side.
- MiniSearch
- fuzzy
- fuzzy-search - an even simpler implementation than microfuzz
- fuzzysearch - tiniest implementation of the list
Author and license
microfuzz was created by @Nozbe.
microfuzz's main author and maintainer is Radek Pietruszewski (website ⋅ twitter ⋅ engineering posters)
microfuzz is available under the MIT license. See the LICENSE file for more info.
No vulnerabilities found.
No security vulnerabilities found.
Other packages similar to @nozbe/microfuzz
@nozbe/simdjson
NPM-released fork of simdjson C++ library
@nozbe/sqlite
Fork of SQLite database - you're probably looking for WatermelonDB, not this
@nozbe/with-observables
<a href="https://opensource.org/licenses/MIT"> <img src="https://img.shields.io/badge/License-MIT-blue.svg" alt="MIT License"> </a>
@nozbe/eslint-plugin-nozbe
A collection of helpful ESLint rules created and used by Nozbe