What is fuzzysort?
Fuzzysort is a fast and powerful fuzzy search library for JavaScript. It allows you to perform fuzzy searching on arrays of strings or objects, providing ranked results based on their similarity to the search query.
What are fuzzysort's main functionalities?
Basic Fuzzy Search
This feature allows you to perform a basic fuzzy search on an array of strings. The results are ranked based on their similarity to the search query 'twl'.
const fuzzysort = require('fuzzysort');
const results = fuzzysort.go('twl', ['twilight', 'twelve', 'two', 'towel']);
console.log(results.map(result => result.target));
Fuzzy Search on Objects
This feature allows you to perform a fuzzy search on an array of objects. The 'key' option specifies which property of the objects to search within.
const fuzzysort = require('fuzzysort');
const books = [
{title: 'The Great Gatsby'},
{title: 'The Grapes of Wrath'},
{title: 'Great Expectations'}
];
const results = fuzzysort.go('grt', books, {key: 'title'});
console.log(results.map(result => result.obj.title));
Highlighting Matches
This feature allows you to highlight the matching parts of the search results. The 'highlight' function wraps the matching parts with the specified HTML tags.
const fuzzysort = require('fuzzysort');
const results = fuzzysort.go('grt', ['The Great Gatsby', 'The Grapes of Wrath', 'Great Expectations']);
results.forEach(result => {
console.log(fuzzysort.highlight(result, '<b>', '</b>'));
});
Other packages similar to fuzzysort
fuse.js
Fuse.js is a lightweight fuzzy-search library that supports searching through arrays of strings and objects. It offers a rich set of options for fine-tuning the search behavior, such as setting the search threshold, specifying keys for object searches, and more. Compared to fuzzysort, Fuse.js provides more configuration options but may be slightly slower in performance.
lunr
Lunr is a full-text search library for JavaScript. It allows you to create an index of your documents and perform searches on that index. Lunr is more suitable for larger datasets and provides more advanced search features like boosting and field-specific search. However, it is more complex to set up compared to fuzzysort.
js-search
JS-Search is a client-side search library that allows you to index and search JavaScript objects. It supports tokenization, stemming, and custom scoring functions. JS-Search is more focused on providing a full-text search experience and is more configurable than fuzzysort, but it may not be as fast for simple fuzzy searches.
Fast, Tiny, & Good fuzzy search for JavaScript.
Fast: <1ms to search 13,000 files.
Tiny: 1 file, 0 dependencies, 5kb.
Good: clean api + sorts results well.
https://rawgit.com/farzher/fuzzysort/master/test/test.html
Installation Node / Bun / Deno
npm i fuzzysort
import fuzzysort from 'fuzzysort'
const fuzzysort = require('fuzzysort')
Installation Browser
<script src="https://cdn.jsdelivr.net/npm/fuzzysort@3.0.2/fuzzysort.min.js"></script>
Usage
fuzzysort.go(search, targets, options=null)
const mystuff = [{file: 'Apple.cpp'}, {file: 'Banana.cpp'}]
const results = fuzzysort.go('a', mystuff, {key: 'file'})
Options
fuzzysort.go(search, targets, {
threshold: 0,
limit: 0,
all: false,
key: null,
keys: null,
scoreFn: null,
})
What's a result
const result = fuzzysort.single('query', 'some string that contains my query.')
result.score
result.target
result.obj
result.indexes
result.highlight('<b>', '</b>')
result.highlight((m, i) => <react key={i}>{m}</react>)
Advanced Usage
Search a list of objects, by multiple complex keys, with custom weights.
let objects = [{
title: 'Liechi Berry',
meta: {desc: 'Raises Attack when HP is low.'},
tags: ['berries', 'items'],
bookmarked: true,
}, {
title: 'Petaya Berry',
meta: {desc: 'Raises Special Attack when HP is low.'},
}]
let results = fuzzysort.go('attack berry', objects, {
keys: ['title', 'meta.desc', obj => obj.tags?.join()],
scoreFn: r => r.score * r.obj.bookmarked ? 2 : 1,
})
var keysResult = results[0]
keysResult[0].highlight()
keysResult[1].highlight()
keysResult.score
keysResult.obj.title
How To Go Fast · Performance Tips
let targets = [{file: 'Monitor.cpp'}, {file: 'MeshRenderer.cpp'}]
targets = targets.filter(t => t.file.length < 1000)
targets.forEach(t => t.filePrepared = fuzzysort.prepare(t.file))
targets = targets.map(t => t.filePrepared)
const options = {
limit: 100,
threshold: .5,
}
fuzzysort.go('gotta', targets, options)
fuzzysort.go('go', targets, options)
fuzzysort.go('fast', targets, options)
Gotcha
result.score
is implemented as a getter/setter and stored different internally
r.score = .3; // r.score == 0.30000000000000004
Star History
Changelog
v3.1.0
- Automatically handle diacritics / accents / ligatures
v3.0.0
- Added new behavior when using
keys
and your search contains spaces! - Added
options.key
can now be a function {key: obj => obj.tags.join()}
- Removed
fuzzysort.indexes
& Added result.indexes
(as a getter/setter for GC perf) - Removed
fuzzysort.highlight()
& Added result.highlight()
- Changed scoring: score is now a number from 0 to 1 instead of from -Infinity to 0
- Changed scoring: substring matches are even more relevant
- Changed scoring:
straw berry
now matches great against strawberry
- Changed scoring: tweaked the scoring quite a bit
result.score
is behind a getter/setter for performance reasons- Fixed minor issues
v2.0.0
- Added new behavior when your search contains spaces!
- Added fuzzysort.min.js
- Now depends on ES6 features
- Removed
result.indexes
& Added fuzzysort.indexes
(improved GC performance) - Completely Removed
options.allowTypo
- Completely Removed
fuzzysort.goAsync
- Completely Removed
fuzzysort.new
- Rewrote the demo
v1.9.0
- Even faster
- Added
options.all
- Deprecated/Removed
options.allowTypo
- Deprecated/Removed
fuzzysort.goAsync
- Changed scoring: boosted substring matches
- Changed scoring: targets with too many beginning indexes lose points for being a bad target
- Changed scoring: penality for not starting near the beginning
- Changed scoring: penality for more groups
- Fixed "Exponential backtracking hangs browser"
v1.2.0
- Added
fuzzysort.highlight(result, callback)
v1.1.0
- Added
allowTypo
as an option
v1.0.0
- Inverted scores; they're now negative instead of positive, so that higher scores are better
- Added ability to search objects by
key
/keys
with custom weights - Removed the option to automatically highlight and exposed
fuzzysort.highlight
- Removed all options from
fuzzysort
and moved them into fuzzysort.go
optional params
v0.x.x