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

sofia-tree

v0.0.9

Published

A fast in-memory dictionary for autocomplete as you type features based on a trie data structure.

Downloads

40

Readme

Sofia Tree

NPM Build Status Coverage Status

A fast-recovery data structure to support auto-complete as you type features.

A kind of Radix Tree or Trie data structure baptized 'Sofia Tree' in honour to my beloved daughter. A multinode tree with a level for each letter with fast lookup operations. See Anottated Source

Features

  • Non-recursive tree traverse implementation to keep memory usage low and speed up on lookup operations.
  • Efficient in-memory data structure, no redis, no dependencies
  • Optional built-in cache for even faster lookups.
  • Optionally case sensitive
  • Optionally limit number of results to get matches even faster
  • Optionally exclude the given prefix if it's a valid word
  • Batteries included example to benchmark its performance

Install

	npm install sofia-tree

Quick and simple usage example

var SofiaTree=require('sofia-tree');

var dictionary=["f","foo","foobar","b","bar","barbar"];

var sofiaTree= new SofiaTree({
	useCache: true,
	caseSensitive: false
});

dictionary.forEach(function(word){
	sofiaTree.insert(word);
},server);
	
//Retrieve all the completions including the prefix
console.log(sofiaTree.getCompletions("foo"));

["foo","foobar"]

//Retrieve all the completions without the prefix
console.log(sofiaTree.getCompletions("foo",true));

["foobar"]

//Limit the number of results without excluding the prefix (defaults to false)
console.log(sofiaTree.getCompletions("f",2)))
["f","foobar"]

//Prints the whole dictionary	
console.log(sofiaTree.getCompletions(""));	
["f","foo","foobar","b","bar","barbar"]

See tests for more complete use cases

A real world example

Clone the whole repo and run npm run example to load a micro search engine, in another console run cd example && ./test.sh to load a 350,000 words dictionary and launch 1,500 searchs with 100 threads in parallel. See Anottated Source and shell scripts

Known limitations

Actually when limiting the maximum number or matches to be retorned it won't necessarely return the first ones in alphabetical order. When returning all the results they won't be alphabetically sorted either. This is due the nature of javascript objects properties which are unordered by definition. Perhaps I'll switch properties for an array in the future.

To be done

  • ~~Define nice jasmine tests and setup Travis~~
  • ~~Comment the example API usage~~
  • Reduce prefixes stack memory comsuption
  • ~~Partial cache invalidation mechanism when inserting new words (it actually resets the whole cache upon insertion of new words.)~~
  • Implement remove method
  • Return results as stream
  • ~~Case-sensitive lookups? Not sure if it's a valuable adition~~
  • Add weight on nodes to enable sort based on some business logic
  • Any ideas? :)

Made with ❤ in Spain