Text Search Engine · MIT

JavaScript Pinyin fuzzy search

Text Search Engine is a JavaScript and TypeScript npm library for searching local strings or objects in the browser or Node.js. A query can mix Chinese characters, full Pinyin, Pinyin initials, and English. Results include offsets into the original text for highlighting.

Install and run a quickstart

npm i text-search-engine

These examples were verified against npm release 1.6.2. The APIs are available from 1.6.0. Save the code as .mjs to run it in Node.js. The examples do not use React, but 1.6.2 declares a React 16.8, 17 or 18 peer: npm may install React 18 automatically, and existing React 19 projects can hit an install conflict.

1. Search an array by Pinyin initials

import { searchItems } from 'text-search-engine'

const [hit] = searchItems(['React', '监控平台'], 'jk')
console.log(hit.item, hit.hitRanges)
// 监控平台, [[0, 1]]

2. Match the title and hostname together

import { createSearcher } from 'text-search-engine'

const pages = [{ title: 'React 监控平台', host: 'github.com' }]
const searcher = createSearcher(pages, {
  getFields: ({ title, host }) => ({ title, host }),
})
const [hit] = searcher.search('jk github')
console.log(hit.fieldHitRanges)
// { title: [[6, 7]], host: [[0, 5]] }

3. Highlight matches in the original text

Continue from example 2. Ranges are inclusive UTF-16 offsets, so add 1 to the end for slice. In the browser, use textContent to fill a <mark> element rather than building HTML from raw text.

const [start, end] = hit.fieldHitRanges.title[0]
console.log(hit.item.title.slice(start, end + 1))
// 监控

Reuse the same createSearcher across keystrokes; rebuild it when the list, field text, or options change. Convert fields to strings and highlight the exact strings you passed in. Empty queries return []; render the original list yourself if desired. Results keep input order by default. Supply sort for your own relevance ranking.

Which to choose: Text Search Engine, pinyin-pro, or Fuse.js?

A side-by-side run on the same data

This is a small comparison, not a general leaderboard. Eight queries cover initials, fields, polyphonic characters, a missing letter, a transposition, and a no-match control. The timing fixture expands seven objects to 1,000 records with numeric suffixes. First query includes setup plus jk; the repeated column averages 24 mixed queries on the same searcher per round. The table reports the median of 31 rounds after discarding two. Rendering, module loading, and normalizing highlight coordinates are excluded.

One run (milliseconds; lower is faster)
ApproachVersionsSetup + first queryRepeated, per query
Text Search Engine1.6.24.291.36
pinyin-pro match3.29.54.776.91
Fuse.js + Pinyin7.5.0 + 3.29.515.025.98
Results for the same queries (selected)
QueryExpectedText Search Enginepinyin-pro matchFuse.js + Pinyin
jk githubReact 监控平台React 监控平台React 监控平台React 监控平台
chongfu重复任务重复任务重复任务重复任务, 银行服务
jvaascriptJavaScriptNoneNoneJavaScript

Run on 2026-11-26 with Node.js 24.22.3 (Linux x64, AMD EPYC 9V74). Only the tested Fuse.js configuration matched JavaScript for jvaascript; it also returned an extra result for chongfu.

This compares specific configurations. Fuse uses threshold: 0.3 and ignoreLocation: true. A different dataset, Pinyin policy, or threshold can change the outcome. See the fixture, full output, and reproduction commands.

Using a coding agent? The project includes an Agent Skill:

npx skills add https://github.com/cjinhuo/text-search-engine --skill text-search-engine