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

@shortwave/trie

v1.1.1

Published

A fast weighted auto-completion trie.

Downloads

552

Readme

A fast weighted auto-completion trie.

Usage


import Trie from '@shortwave/trie';

const trie = new Trie<Contact>();

trie.add({
    key: "richard",
    value: {name: "Richard", ...},
    score: 5
});

trie.add({
    key: "rachael",
    value: {name: "Rachael", ...},
    score: 1
});

trie.add({
    key: "sarah",
    value: {name: "Sarah", ...},
    score: 3
});

trie.add({
    key: "sam",
    value: {name: "Sam", ...},
    score: 2
});

// The limit option limits the number of results.
// The unique option returns only the first result for each key
// (the trie can store multiple values per key)
trie.prefixSearch('r', {limit: 3, unique: true})
//=> [{name: "Richard", ...} , {name: "Rachael", ...}]


// You can also remove nodes from the tree.
trie.remove({key: "sarah"});

Efficiency

The problem with building a search tree for contacts is that the order of results is orthogonal from the search.

If I type "R", my dad ("Richard") should be the first auto-completion result, not my friend "Rachael" who I haven't spoken to for a year.

To do this nodes in the trie have their highest score attached:

                     + (4) rachael
           + (4) r --+ (1) richard
  root ----+
           + (3) s ---+ (3) a ---+ (3) sarah
                                 + (2) sam

To improve indexing time, sub-nodes are not sorted; so on the first access to each section of the tree you have to pay the cost of sorting the sub-nodes ( this is usually a very small sort, <5 entries, per node in the tree).

There's also a special case for nodes with one child, where we don't expand them out letter by letter. (An exercise for the reader would be to use a fully compressed trie, which would give us this optimization as a side-effect)

The search algorithm maintains a queue of nodes to visit based on score, and is truncated to the limit. The most expensive part of the search is merging the current node's children into the priority queue, and so the efficiency of lookup is dominated by the limit parameter, with some additional cost due to having to traverse many intermediate nodes (see note about implementing a compressed trie).

Meta-fu

This code base started out as the trie implementation from https://github.com/superhuman/trie-ing with changes for typescript and deletion.

See LICENSE for original copyright holder, changes are also released under the MIT license.