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

tynisearch

v1.0.11

Published

Tiny search module based on trie and aho-corasick using TypeScript

Downloads

38

Readme

About

  • Tiny, and simple search module based on trie and aho-corasick using TypeScript.
  • Also, it supports serialization and de-serialization into string type.

Use case

  • I made this library for searching keywords my users enrolled from a title of a post without search engine library.

For me?

  • Making trie based on keywords of users
  • Save the serialized trie into in memory k-v storage like Redis.
  • Retrieve the serialized trie from the storage and de-serialize it when.
    • User enrolled new keywords
    • User deleted existed keywords
  • Search keywords in a title of a post using de-serialized trie

Installation

npm install tynisearch

Benchmark

TL;DR

result-of-dataset

In table

| Calculation | Elapsed time (ms) | |----------------------------------------------|-----------------------| | Searching | 1 | | Inserting (including building failure links) | 60 | | Serialization and de-serialization | 90 |

Dataset Description

  • 10,000 words, which consisted of words longer than 5 characters

Test Description

  • You can see the performance test code here

Example

Insert and delete

import { TyniSearch } from 'tynisearch';

const tyniSearch = new TyniSearch();

const words = ["fox", "dog"]
const titleToSearch = "The quick brown fox jumps over the lazy dog";

// insert words as a list
tyniSearch.insert(words);

const result = tyniSearch.searchInSentence(titleToSearch); 
console.log(result); // ["fox", "dog"]

// delete words as a list
tyniSearch.delete(words);

const resultAfterDeletion = tyniSearch.searchInSentence(titleToSearch);
console.log(resultAfterDeletion); // []

Serialization and De-serialization (SerDe)

const tyniSearch = new TyniSearch();

const wordList = ["fox", "dog"]
const titleToSearch = "The quick brown fox jumps over the lazy dog";

// insert words as a list
tyniSearch.insert(words);

// serialize to string
const ser = tyniSearch.serialize();

// de-serialize from serialized string
const de = TyniSearch.deserialize(ser);

// search in de-serialized trie
const result = de.serialize(titleToSearch);
console.log(result); // ["fox", "dog"]

Optional failure link calculation

  • By default, failure links are calculated when inserting, parsing, and deleting.
  • You can disable this behavior by passing false to the second argument() of their methods.
const wordList = ["fox", "dog", "cat", "cow", "doll"];
const anotherWordList = ["elephant", "tiger", "lion", "wolf", "bear"];

const tyniSearch = new TyniSearch();

// If you should insert and delete keywords frequently,
// you can disable failure link calculation to enhance performance.
tyniSearch.insert(keywordList, false);
tynisearch.delete(["fox", "cat"], false);
tynisearch.delete(["dog", "cow"], false);
tyniSearch.insert(anotherWordList, false);

// after all, you should build failure links manually
tyniSearch.buildFailureLinks();
const wordList = ["fox", "dog", "cat", "cow", "doll"];
const tyniSearch = new TyniSearch();

tyniSearch.insert(wordList);

const ser = tyniSearch.serialize();

// If you have to insert deserialize trie frequently,
// also you can disable failure link calculation to enhance performance.
const de = TyniSearch.deserialize(ser, false);

const anotherWordList = ["elephant", "tiger", "lion", "wolf", "bear"];
de.insert(anotherWordList, false);

const secondSer = de.serialize();

// like above, deserialize without buildFailureLinks param automatically builds failure links
const secondDe = TyniSearch.deserialize(secondSer);

Misc.

const tyniSearch = new TyniSearch();
const words = ["fox", "dog"]
tyniSearch.insert(words);

// getting the number of all nodes
tyniSearch.getNumberOfNodes(); // 2

// getting all saved keywords in trie
tyniSearch.getAllKeywords(); // ["fox", "dog"]

Points should be enhanced

Can not save failure links in serialization and de-serialization

Because of the circular reference problem, I can not save failure links in serialization and de-serialization. And I think there must be a better way to solve this problem.

You should build failure links using .buildFailureLinks() again after de-serialization.

Or, you can contribute to this project to solve this problem (I really want it!)

Calculating the failure links is not efficient

I think it is also better way to calculate failure graph when building failure links.

Currently, building failure graph is required after every insertion. In other words, you should call .buildFailureLinks() before run searchInSentence(sentence: string) method after any new words are inserted into.

Or, you can contribute to this project to solve this problem (I really want it!)(2)


Contacts

Email

github

  • https://github.com/prravda

LinkedIn

  • https://www.linkedin.com/in/pravdakracota/