Repo of the Day
thesephist/libsearch
Published: Sep 16, 2022
Open repository ↗A search library for JS
Summary
libsearch is a small JavaScript library that adds full-text search to in-memory lists of objects without requiring a pre-built search index. It exposes a single search() function that filters items by matching a query against a chosen string field and ranks results with an approximate TF-IDF score. The library ships with TypeScript types and works in both Node.js and the browser.
What it is useful for
libsearch sits between String.prototype.indexOf and full search engines like lunr.js or FlexSearch. It is a good fit when you have a few MBs of data that already lives in memory (a list of articles, notes, todo items, contacts, or similar) and you want to filter and rank that list as the user types. The README describes it being used in products like YC Vibe Check and personal productivity software, which suggests client-side search on small to medium datasets.
Because there is no indexing step, the library works the moment your data is loaded. That makes it practical for prototypes, single-page apps, browser extensions, and Electron apps, where shipping and maintaining a separate index would be overhead. It supports three matching modes: word (whole-word matches), prefix (each query token may match the start of a word), and autocomplete (whole-word for all but the last token, which may be a prefix), as well as a caseSensitive toggle.
Documented limitations: libsearch performs an O(n) scan on every search and approximates TF-IDF using document character length rather than token count, so it is not designed for very large corpora. For those cases, a dedicated library like FlexSearch or a server-side search system is more appropriate, as the README itself notes.
How engineers can use it
Install via npm or yarn:
npm install libsearch
In Node.js or a bundler:
import { search } from 'libsearch';
const results = search(items, 'berkeley cali', item => item.title, { mode: 'prefix' });
In the browser without a bundler, add the script tag to your HTML:
<script src="https://unpkg.com/libsearch/dist/browser.js"></script>
Then call window.libsearch.search(...) from your code.
The required arguments are the array of items and the query string. The third argument is a function that extracts the searchable string from each item; it defaults to String(item), which works for arrays of plain strings. The optional options object controls case sensitivity and matching mode. To build from source, the README documents yarn build:all to produce both the ES module and browser builds, and yarn test (run after yarn build:tsc) to execute the unit test suite.