Code Monkey home page Code Monkey logo

fast-matcher's Introduction

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.

fast-matcher's People

Contributors

dtao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

vsch cybernetics

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.