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

lucid-suggest

v0.8.3

Published

Fulltext search and autocomplete engine that works out of the box.

Downloads

11

Readme

LucidSuggest

Autocomplete engine that works out of the box. Fast, easy to use, runs in browsers and Node.js. Built with Rust and WebAssembly.

Note: this package is pre-1.0, it hasn't been battle-tested in production, API may change.

Table of contents

Resources

Live demo.

API reference.

Code examples:

Getting started

Install:

npm install lucid-suggest

Initialize:

import {LucidSuggest} from 'lucid-suggest/en'

const suggest = new LucidSuggest()
suggest.addRecords([
    {id: 1, title: "Electric Toothbrush"},
    {id: 2, title: "Lightning to USB-C Cable"},
    {id: 3, title: "AA Alkaline Batteries"},
])

Search:

await suggest.search("batteries")
// returns:
// [
//   Hit { title: "AA Alkaline [Batteries]" }
// ]

Rendering results

By default LucidSuggest highlights hit titles using [ ]. The easiest way to change it is to use highlight helper function:

import {LucidSuggest, highlight} from 'lucid-suggest'

const suggest = new LucidSuggest()
const hits = await suggest.search("to")

hits.map(hit => ({
    value: hit.record.id.toString(),
    label: highlight(hit, '<strong>', '</strong>')
}))
// returns:
// [
//   {value: "1", label: "Electric <strong>To</strong>othbrush"},
//   {value: "2", label: "Lightning <strong>to</strong> USB-C Cable"},
// ]

Or you can directly operate on chunks of a highlighted text, which can come in handy if you need a more complex render logic:

const hits = await suggest.search("to")
hits.map(hit => ({
    value: hit.record.id.toString(),
    label: hit.chunks
        .map(c => c.highlight ? `<strong>${c.text}</strong>` : c.text)
        .join('')
}))
// returns:
// [
//   {value: "1", label: "Electric <strong>To</strong>othbrush"},
//   {value: "2", label: "Lightning <strong>to</strong> USB-C Cable"},
// ]

For examples of rendering in React or Vue, see Resources section.

Fulltext search features

When an exact match is unavailable, the best possible partial matches are returned:

await suggest.search("plastic toothbrush")
// returns:
// [
//   Hit { title: "Electric [Toothbrush]" }
// ]

Search as you type, results are provided from the first letter:

await suggest.search("c")
// returns:
// [
//   Hit { title: "Lightning to USB-C [C]able" }
// ]

Search algorithm is resilient to different kinds of typos:

await suggest.search("alkln")
// returns:
// [
//   Hit { title: "AA [Alkalin]e Batteries" }
// ]
await suggest.search("tooth brush")
// returns:
// [
//   Hit { title: "Electric [Toothbrush]" }
// ]

Stemming is used to handle different word forms:

await suggest.search("battery")
// returns:
// [
//   Hit { title: "AA Alkaline [Batteri]es" }
// ]

Function words (articles, prepositions, etc.) receive special treatment, so they don't occupy top positions every time you start typing a word:

await suggest.search("to")
// returns:
// [
//   Hit { title: "Electric [To]othbrush" },
//   Hit { title: "Lightning [to] USB-C Cable" },
// ]

Rating

Optional rating field can be used as a tie breaker: records with greater rating are ranked higher. Use priority, product popularity, or term frequency as rating to improve overall scoring.

For example, let's use state population as rating:

suggest.addRecords([
    {id: 1, rating:  3000, title: "Nevada"},
    {id: 2, rating:  8900, title: "New Jersey"},
    {id: 3, rating: 19500, title: "New York"},
])
await suggest.search("ne")
// returns:
// [
//   Hit { title: "[Ne]w York" },
//   Hit { title: "[Ne]w Jersey" },
//   Hit { title: "[Ne]vada" },
// ]

Supported languages

| Language | Module | | :--------- | :----------------- | | German | lucid-suggest/de | | English | lucid-suggest/en | | French | lucid-suggest/fr | | Spanish | lucid-suggest/es | | Portuguese | lucid-suggest/pt | | Russian | lucid-suggest/ru |

Bundle sizes

Note that base64-encoded Wasm source constitutes the bulk of a bundle. Minifiers don't affect it, but gzip compresses it well.

| lang | size | gzipped | | :--- | ---: | ------: | | de | 200K | 79K | | en | 202K | 80K | | es | 205K | 80K | | es | 208K | 82K | | pt | 205K | 81K | | ru | 202K | 79K |

Performance

LucidSuggest works best with shorter sentences, like shopping items or book titles. Using longer texts, like articles or movie descriptions, may lead to performance regressions and generally poor user experience.

LucidSuggest can handle large dataset. For example, for 10000 records, each containing 4-8 common English words, you can expect a typical search to take about 1 ms, so you can simply call it at every keystroke without using throttling or Web Workers.

Below are the detailed performance measurements, obtained using Node.js 14.3, Intel Core i7 (I7-9750H) 2.6 GHz.

Searching:

| | 2-4 words | 4-8 words | | --------------: | --------: | --------: | | 100 records | 0.09 ms | 0.24 ms | | 1000 records | 0.27 ms | 0.48 ms | | 10 000 records | 0.45 ms | 0.68 ms | | 100 000 records | 1.40 ms | 2.00 ms |

Indexing:

| | 2-4 words | 4-8 words | | --------------: | --------: | --------: | | 100 records | 1 ms | 2 ms | | 1000 records | 11 ms | 16 ms | | 10 000 records | 88 ms | 160 ms | | 100 000 records | 810 ms | 1900 ms |