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

search-optimized-filter

v1.0.6

Published

Efficiently search through arrays using a Trie-based implementation for fast and optimized lookups.

Downloads

453

Readme

TrieSearch - Efficient String Search Using Trie

TrieSearch is a lightweight and high-performance JavaScript library that uses the Trie (prefix tree) data structure to efficiently search through arrays of strings or objects. It supports searching with blazing speed, capable of finding results in large datasets (e.g., 20,000 words) in under 1 millisecond.


🚀 Features

  • 🔍 Fast Search: Efficiently search through large datasets using Trie.
  • 📂 Flexible Input: Supports arrays of strings or arrays of objects (with a specified key).
  • 🛠 Customizable: Easily adapt to different use cases by providing the field to search for in object arrays.
  • Performance: Handles thousands of entries in milliseconds.

📥 Installation

Install the package using npm:

npm install search-optimized-filter

📘 Usage

1. Searching an Array of Strings

   import TrieSearch from "search-optimized-filter";
   const words = ["hello", "world", "hii", "trie", "search", "how are you"];
   const trie = new TrieSearch(words);
   // Search for words starting with "h"
   console.log(trie.suggest("h")); // Output: ["hii","hello","how are you"] 

2. Searching an Array of Objects If the input is an array of objects

specify the key to search for:

   import TrieSearch from "search-optimized-filter";
  
   const data = [
     { name: "john", age: 10 },
     { name: "jane", age: 20 },
     { name: "mark", age: 23 },
     { name: "bob", age: 25 },
     { name: "alliss", age: 5 },
     { name: "aston", age: 30 },
   ];
  
   const trie = new TrieSearch(data, "name");
  
   // Search for names starting with 'a'
   console.log(trie.suggest("a")); // Output: ["alliss", "aston"]

[!IMPORTANT] Initialize the TrieSearch object in the parent component to avoid regenerating the trie on every render, as building the trie is the most time-consuming operation and is unnecessary if the data doesn't change.

⚙️ API Reference

Constructor

new TrieSearch(array, [key])
  • array: The dataset to be used. Can be an array of strings or an array of objects.
  • key: (Optional) The key in the object to search for (required if the input is an array of objects).

Methods

  1. insert(word) Inserts a word into the Trie.(Internally used; rarely needed directly).
  2. suggest(prefix) Returns an array of words or object values that match the given prefix.
  • prefix: The prefix string to search for.

💡 Optimization Tip

If you're using TrieSearch in a React or similar framework where state updates may trigger re-renders, consider initializing the TrieSearch object once in a parent or higher-level component.

This prevents unnecessary re-creation of the Trie structure on every render, especially when the words array remains unchanged. Since generating the Trie is the most time-intensive operation, this approach ensures optimal performance.

Example:

Parent Component

   import React, { useState } from "react";
   import TrieSearch from "trie-search";
   import Child from "./Child";
   
   const Parent = () => {
     const words = ["apple", "banana", "grape", "orange", "mango"];
     const trie = new TrieSearch(words); // Create TrieSearch object here
     const [searchTerm, setSearchTerm] = useState("");
   
     return (
       <div>
         <h1>TrieSearch Example</h1>
         <input
           type="text"
           value={searchTerm}
           onChange={(e) => setSearchTerm(e.target.value)}
           placeholder="Type to search..."
         />
         <Child trie={trie} searchTerm={searchTerm} />
       </div>
     );
   };
   
   export default Parent;

Child Component

import React from "react";

const Child = ({ trie, searchTerm }) => {
  const results = trie.suggest(searchTerm); // Use the suggest function

  return (
    <div>
      <h2>Search Results:</h2>
      <ul>
        {results.map((result, index) => (
          <li key={index}>{result}</li>
        ))}
      </ul>
    </div>
  );
};

export default Child;

🏎️ Performance

TrieSearch is optimized for high performance, capable of searching 20,000 words in under 1 millisecond.
Here's a performance screenshot:

🌟 Why Use TrieSearch?

  • Perfect for autocomplete, predictive text, or any search functionality requiring lightning-fast results.
  • Easy-to-use API with seamless integration into existing JavaScript or Node.js projects.
  • Flexible handling of both arrays of strings and arrays of objects.

📄 Example Code

import TrieSearch from "search-optimized-filter";

// Example 1: Array of Strings
const words = ["apple", "banana", "grape", "orange", "mango"];
const trie1 = new TrieSearch(words);
console.log(trie1.suggest("gr")); // Output: ["grape"]

// Example 2: Array of Objects
const data = [
  { name: "alice", age: 30 },
  { name: "bob", age: 25 },
  { name: "charlie", age: 35 },
];
const trie2 = new TrieSearch(data, "name");
console.log(trie2.suggest("al")); // Output: ["alice"]

📦 Package Details

  • Language: JavaScript (Node.js)

  • Dependencies: None

🛠️ Contributing

Contributions, issues, and feature requests are welcome! Feel free to fork the repository and submit pull requests.

📃 License

This project is licensed under the MIT License.

Start optimizing your search today with TrieSearch! 🚀