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

js-native-ternary-buffer-tree

v0.2.0

Published

C++ ternary search tree, for Node Buffers

Downloads

7

Readme

js-native-ternary-buffer-tree

A native Node module implementing an ordered mapping of Buffers to Objects.

The goal is for super-duper-fast string matching. That means inputs and outputs are always Buffers.

The implementation is a ternary search tree. That strikes a good balance: memory usage isn't as excessive as with a hash table, and lookups aren't as slow as with a binary search tree.

Usage

var TernaryBufferTree = require('js-native-ternary-buffer-tree');

// Use as a set. Input is a single Buffer: newline-separated Strings.
//
// Performance will be best if the Strings are sorted in UTF-8 byte order.
var tree = new TernaryBufferTree(new Buffer('bar\nbaz\nfoo\nmoo\nthe foo', 'utf-8'));

console.log(tree.contains('foo')); // true
console.log(tree.contains('moo')); // false

// Use as a map. Input is a single Buffer: tabs separate keys from values.
//
// When a key is queried without a value, `.get()` returns `null`. When a key
// does not exist, `.get()` returns `undefined`.
//
// Performance will be best if input keys are sorted in UTF-8 byte order.
var tree = new TernaryBufferTree(new Buffer('foo\tFOO\nbar\tBAR\nbaz\tBAZ\nx\ny', 'utf-8'));

console.log(tree.get('foo')); // 'FOO'
console.log(tree.get('x')); // null -- exists but has no value
console.log(tree.get('moo')); // undefined

// Super-optimized method that scratches an itch we had
//
// You can insert multi-word tokens and then query for single-word or
// multi-word combinations. The dataset needn't have any values; the query
// words must be space-separated.
var tree = new TernaryBufferTree(new Buffer('bar\nbaz\nfoo\nmoo\nthe foo', 'utf-8'));
console.log(tree.findAllMatches('the foo drove over the moo', 2)); // [ 'the foo', 'foo', 'moo' ]

contains(key)

.contains(key) is equivalent to .get(key) !== undefined.

key may be a String or a UTF-8 Buffer. Buffer logic should be more efficient, as String input will be encoded as UTF-8 internally.

get(key)

You can use TernaryBufferTree as a set or as a map. The input Buffer always contains one key per line, but if you add a tab and then a value, the get() method will return the given value for the given key.

If the key was provided without a value, .get(key) will return null. If the key was not provided, .get(key) will return undefined.

key may be a String (and .get(key) will return a String); or it may be a UTF-8 Buffer (and .get(key) will return a Buffer). String types imply UTF-8 encoding and decoding; Buffer types are more direct because the tree data is stored as UTF-8.

findAllMatches(tokens, maxNgramSize)

This method is interesting in that it can search for tokens that span multiple words (the second argument specifies the number of words), in a memory-efficient manner. The memory used is the size of the output Array. The time complexity is on the order of the size of the input times the number of tokens.

tokens may be a String (and .findAllMatches(tokens, n) will return an Array of Strings); or it may be a UTF-8 Buffer (and .findAllMatches(tokens, n) will return an Array of Buffers). String types imply UTF-8 encoding and decoding; Buffer types are more direct because the tree data is stored as UTF-8.

Developing

Download, npm install.

Run mocha -w in the background as you implement features. Write tests in test and code in src.

LICENSE

AGPL-3.0. This project is (c) Overview Services Inc., Overview Computing Inc. Please contact us should you desire a more permissive license.