PitchHut
Log in / Sign up
fuzzysort
7 views
Effortless fuzzy search for JavaScript with unrivaled speed.
Pitch

Fuzzysort delivers a fast and efficient fuzzy search experience for JavaScript, capable of searching 13,000 files in under 1ms. At just 5KB with no dependencies, it offers a clean API and effective result sorting. Simplify your search tasks in any JavaScript environment and enhance your productivity effortlessly.

Description

Fuzzysort is an exceptionally fast and lightweight fuzzy search library designed for JavaScript, allowing developers to implement Sublime Text-like search functionalities effortlessly.

Key Features

  • Speed: Achieve blazing fast search results in under 1ms across 13,000 files.
  • Size: Just a single file with no external dependencies, weighing only 5kb.
  • Quality: Offers a clean API and sorts results intelligently for optimal user experience.

Demo

Check out the live demo here to see the functionality in action!

Screenshot Gallery

Demo GIF Search Result Example Different Search Case Additional Use Case

Usage

Using Fuzzysort is straightforward. Here’s a quick example:

const mystuff = [{file: 'Apple.cpp'}, {file: 'Banana.cpp'}];
const results = fuzzysort.go('a', mystuff, {key: 'file'});
// [{score: 0.81, obj: {file: 'Apple.cpp'}}, {score: 0.59, obj: {file: 'Banana.cpp'}}]

Options and Customization

Fuzzysort allows for advanced search configurations to tailor the results to your needs:

fuzzysort.go('searchTerm', targets, {
  threshold: 0,  // Minimum score to return results
  limit: 0,      // Limit the number of results returned
  all: false,    // Return all results for an empty search if true
  key: null,     // Specify key when searching objects
  keys: null,    // Specify multiple keys when searching objects
  scoreFn: null, // Custom scoring function for keys
});

Advanced Features

Fuzzysort supports searching across multiple keys with weighted scoring:

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), // Boost score for bookmarked items
});

Performance Tips

To maintain optimal performance:

  • Filter out unnecessary targets before searching.
  • Utilize prepared targets for static collections.
  • Adjust options to limit the number of results and set a suitable score threshold.

Conclusion

Fuzzysort stands out as an efficient solution for implementing fuzzy search in JavaScript applications. Whether you're building a simple app or a sophisticated tool, Fuzzysort provides the speed and flexibility needed to enhance search functionalities.