npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fast-matcher

v0.3.1

Published

Find matches fast

Downloads

3,590

Readme

fast-matcher

Most autocomplete implementations I've seen do full-list scanning and simple (infix) text matching.

The standard performance optimizations seem to be complex: indexing, caching, tries, etc.

This library is a simpler and much faster "back end" for autocompletes. It isn't as full-featured, which means it can make some assumptions for really big perf wins, namely:

  1. It only does prefix matching.

In my experience this actually makes perfect sense in a lot of cases. For example suppose your user is searching for a company name; they aren't going to type "o" or "e" to search for Home Depot. Or say they're using a dictionary; they aren't going to type "n" to search for anagram.

See for yourself how fast this is in practice: the demo uses this library to provide autocomplete results from WordNet, with close to 150,000 words. Results are pretty much instantaneous.

Usage

// constructor

var matcher = new FastMatcher(list, options);

// getting matches

var matches = matcher.getMatches(prefix);

// full example

var list = [
  { x: 'foo' },
  { x: 'bar' },
  { x: 'baz' }
];

var matcher = new FastMatcher(list, {
  // the property, or array of properties, to base matches on
  selector: 'x',

  // duh, what do you think this does?
  caseInsensitive: true,

  // return matches in their original order
  preserveOrder: true,

  // whether to match against any word (not just first) for each string
  anyWord: false,

  // how many matches to find at a time
  limit: 25
});

matcher.getMatches('ba');
// => [{ x: 'bar' }, { x: 'baz' }]

How does it work?

It's really not complicated. When you construct a FastMatcher instance, it creates a copy of the source list, sorted (by the property you specified with the selector option, if provided). Then when you call getMatches it does a binary search for the given prefix in the sorted list, and just iterates from that spot until (a) reaching the limit or (b) hitting an item that doesn't match.

Binary search is really, really fast.

Has this been done before?

Maybe. Probably. I don't know.