Convert Figma logo to code with AI

farzher logofuzzysort

Fast SublimeText-like fuzzy search for JavaScript.

4,350
160
4,350
7

Top Related Projects

20,419

Lightweight fuzzy-search, in JavaScript

:crystal_ball: Tiny and blazing-fast fuzzy search in JavaScript

fuzzyset.js - A fuzzy string set for javascript

Simple, expected, and deterministic best-match sorting of an array in JavaScript

Quick Overview

Fuzzysort is a fast, lightweight JavaScript fuzzy search library. It provides efficient and accurate fuzzy matching for strings, making it ideal for implementing search functionality in web applications or command-line interfaces.

Pros

  • Fast performance, even with large datasets
  • Lightweight with no dependencies
  • Supports both browser and Node.js environments
  • Customizable scoring and highlighting options

Cons

  • Limited to string-based searches
  • May not be suitable for complex search scenarios
  • Documentation could be more comprehensive
  • No built-in support for asynchronous operations

Code Examples

Basic fuzzy search:

import fuzzysort from 'fuzzysort'

const result = fuzzysort.single('al', 'apple')
console.log(result.score) // Output: 0
console.log(result.target) // Output: 'apple'

Searching through an array of strings:

const fruits = ['apple', 'banana', 'orange', 'pear']
const results = fuzzysort.go('an', fruits)
console.log(results.map(r => r.target)) // Output: ['banana', 'orange']

Custom key search on objects:

const books = [
  { title: 'The Great Gatsby', author: 'F. Scott Fitzgerald' },
  { title: 'To Kill a Mockingbird', author: 'Harper Lee' }
]
const results = fuzzysort.go('kill', books, {key: 'title'})
console.log(results[0].obj.title) // Output: 'To Kill a Mockingbird'

Getting Started

  1. Install fuzzysort:

    npm install fuzzysort
    
  2. Import and use in your project:

    import fuzzysort from 'fuzzysort'
    
    const searchTerm = 'fzz'
    const targets = ['fuzzy', 'fuzzysort', 'algorithm']
    
    const results = fuzzysort.go(searchTerm, targets)
    console.log(results.map(r => r.target))
    
  3. For more advanced usage, refer to the GitHub repository for additional options and configurations.

Competitor Comparisons

20,419

Lightweight fuzzy-search, in JavaScript

Pros of Fuse

  • More comprehensive and feature-rich fuzzy search library
  • Supports searching through objects and nested properties
  • Offers more customization options for search behavior

Cons of Fuse

  • Generally slower performance compared to fuzzysort
  • Larger file size, which may impact load times in web applications
  • More complex API, potentially requiring a steeper learning curve

Code Comparison

Fuse:

const fuse = new Fuse(list, {
  keys: ['title', 'author.firstName']
})
const result = fuse.search('old man')

fuzzysort:

const results = fuzzysort.go('old man', list, {
  keys: ['title', 'author.firstName']
})

Both libraries offer similar basic functionality, but Fuse provides more advanced options for configuring the search behavior. fuzzysort's API is simpler and more straightforward, which may be preferable for simpler use cases.

While Fuse offers more features and flexibility, fuzzysort focuses on speed and simplicity. The choice between the two depends on the specific requirements of your project, such as the need for advanced search capabilities versus performance optimization.

:crystal_ball: Tiny and blazing-fast fuzzy search in JavaScript

Pros of fuzzysearch

  • Lightweight and simple implementation
  • Supports searching with typos and character transpositions
  • Works well for small to medium-sized datasets

Cons of fuzzysearch

  • Less optimized for large datasets compared to fuzzysort
  • Fewer advanced features and customization options
  • May not perform as well for complex sorting scenarios

Code Comparison

fuzzysearch:

function fuzzysearch (needle, haystack) {
  var hlen = haystack.length;
  var nlen = needle.length;
  if (nlen > hlen) {
    return false;
  }
  // ... (additional implementation)
}

fuzzysort:

function fuzzysort(search, target, options) {
  if (!search) return null
  if (!target) return null

  var preparedSearch = fuzzysort.prepareSearch(search)
  var searchLowerCode = search[0].toLowerCase().charCodeAt(0)
  // ... (additional implementation)
}

Both libraries provide fuzzy searching functionality, but fuzzysort offers more advanced features and optimizations for larger datasets. fuzzysearch is simpler and more lightweight, making it suitable for smaller projects or when minimal dependencies are desired. fuzzysort provides better performance for complex sorting scenarios and larger datasets, but may be overkill for simpler use cases.

fuzzyset.js - A fuzzy string set for javascript

Pros of fuzzyset.js

  • Supports more advanced fuzzy matching techniques, including Levenshtein distance
  • Provides a confidence score for matches, allowing for more fine-grained control
  • Can handle larger datasets efficiently due to its indexing approach

Cons of fuzzyset.js

  • Generally slower performance compared to fuzzysort, especially for simpler matching tasks
  • More complex API, which may be overkill for basic fuzzy search needs
  • Larger file size, potentially impacting load times in web applications

Code Comparison

fuzzyset.js:

const FuzzySet = require('fuzzyset.js');
const set = FuzzySet(['apple', 'banana', 'orange']);
const result = set.get('aple');
console.log(result); // [[0.8, 'apple']]

fuzzysort:

const fuzzysort = require('fuzzysort');
const fruits = ['apple', 'banana', 'orange'];
const result = fuzzysort.go('aple', fruits);
console.log(result[0].target); // 'apple'

Both libraries offer fuzzy string matching capabilities, but they differ in their approach and use cases. fuzzyset.js provides more advanced matching techniques and confidence scores, making it suitable for complex fuzzy matching scenarios. However, it comes at the cost of performance and simplicity. fuzzysort, on the other hand, offers a simpler API and faster performance, making it ideal for basic fuzzy search functionality in applications where speed is crucial.

Simple, expected, and deterministic best-match sorting of an array in JavaScript

Pros of match-sorter

  • More flexible ranking system with customizable criteria
  • Supports internationalization and diacritics
  • Better documentation and examples

Cons of match-sorter

  • Slightly slower performance for large datasets
  • More complex API, which may be overkill for simple use cases

Code Comparison

match-sorter:

import matchSorter from 'match-sorter'

const results = matchSorter(list, 'query', {
  keys: ['name', 'email'],
  threshold: matchSorter.rankings.CONTAINS
})

fuzzysort:

import fuzzysort from 'fuzzysort'

const results = fuzzysort.go('query', list, {
  keys: ['name', 'email']
})

Both libraries provide fuzzy searching capabilities, but match-sorter offers more advanced features and customization options. It supports multiple ranking criteria and internationalization, making it suitable for complex search scenarios. However, this comes at the cost of slightly lower performance and a more complex API.

fuzzysort, on the other hand, is designed for simplicity and speed. It's more lightweight and performs better with large datasets, but lacks some of the advanced features found in match-sorter.

The choice between the two depends on the specific requirements of your project, balancing between flexibility and performance.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

fuzzysort

Fast, tiny, good fuzzy search for JavaScript.
<1ms for 13,000 files · 0 dependencies · clean ranking

Demo

https://farzher.github.io/fuzzysort/test/test.html

Install

npm i fuzzysort
import fuzzysort from 'fuzzysort'

Browser:

<script type="module">
  import fuzzysort from 'https://cdn.jsdelivr.net/npm/fuzzysort@4.0.2/fuzzysort.min.js'
</script>

Quick start

const files = [
  {file: 'Guide.cpp'},
  {file: 'UserInterface.cpp'},
]

const results = fuzzysort.go('ui', files, {key: 'file'})

results[0].obj.file // 'UserInterface.cpp'
fuzzysort.go(search, targets, {
  limit     : 10,   // Max results; 0 = unlimited
  threshold : .5,   // Minimum score; 0 = any match
  key       : null, // Search one property
  keys      : null, // Search multiple properties
  scoreFn   : null, // Override result scoring
})

Results

const result = fuzzysort.single('query', 'some string that contains my query.')
result.score   // .80 (1 is a perfect match. 0.5 is a good match. 0 is no match.)
result.target  // 'some string that contains my query.'
result.indexes // [29, 30, 31, 32, 33]
result.obj     // reference to your original obj when using options.key

result.highlight('<b>', '</b>') // 'some string that contains my <b>query</b>.'
result.highlight((m, i) => <react key={i}>{m}</react>)

Advanced Usage - multiple complex keys - custom scoring

const objects = [{
  title: 'Petaya Berry',
  meta: {desc: 'Raises Special Attack when HP is low.'},
  tags: ['berries', 'items'],
}, {
  title: 'Liechi Berry',
  meta: {desc: 'Raises Attack when HP is low.'},
  favorite: true,
}]

const targets = fuzzysort.snapshot(objects, {
  keys: ['title', 'meta.desc', obj => obj.tags?.join()],
})

const results = fuzzysort.go('attack berry', targets, {
  scoreFn: r => r.score * (r.obj.favorite ? 2 : 1),
})

const result = results[0]
result[0].highlight() // 'Liechi <b>Berry</b>'
result[1].highlight() // 'Raises <b>Attack</b> when HP is low.'
result.obj.title      // 'Liechi Berry'

How To Go Fast! - Performance Tips!

Filter out targets you don't need to search! Especially long ones!

let targets = [
  {file: 'Guide.cpp'},
  {file: 'UserInterface.cpp'},
].filter(t => t.file.length < 1000)

If your targets don't change, take an immutable snapshot() for the best performance!

targets = fuzzysort.snapshot(targets, {key: 'file'})

fuzzysort.go('gotta', targets)
fuzzysort.go('go',    targets)
fuzzysort.go('fast',  targets)

If you can't snapshot, provide prepared targets instead of raw strings!

targets.forEach(t => t.filePrepared = fuzzysort.prepare(t.file))

fuzzysort.go('fast', targets, {key: 'filePrepared'})

Character remapping

Searches use NFKD normalization, strip diacritics, and remap common quote, dash, slash, ellipsis, and lookalike characters.

Add or override mappings with fuzzysort.remap():

fuzzysort.remap({',': '.'}) // 12,5 = 12.5

Web Workers / cloned results

Structured cloning can remove the getters on results. For cloned results use:

fuzzysort.score(result)
fuzzysort.highlight(result)

Changelog

v4.0.0

  • ESM instead of UMD
  • Added fuzzysort.snapshot() for the best search performance
  • Added fuzzysort.score() and fuzzysort.highlight() for Web Worker support
  • Added fuzzysort.remap() for custom normalization
  • Automatically remaps common lookalike characters
  • Added default threshold and limit
  • Improved multi-key highlighting
  • Improved substring scoring
  • Removed options.all; empty search now returns results
  • scoreFn now works with all search modes

NPM DownloadsLast 30 Days