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?
- Text Search Engine:Choose it for an in-memory list where Pinyin, English, object fields, and original-text highlighting need to work together. It is not a full-text index or a general spell corrector.
- pinyin-pro match:Choose it for matching individual strings or for transliteration, with your own list traversal and ranking.
match()also returns positions; original-text highlighting is not unique to Text Search Engine. - Fuse.js + Pinyin:Start with Fuse.js if typo tolerance and field weights matter more. Add transliterated and initial fields for Pinyin search, and map offsets in those derived fields back to the original text if you need highlights there.
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.
| Approach | Versions | Setup + first query | Repeated, per query |
|---|---|---|---|
| Text Search Engine | 1.6.2 | 4.29 | 1.36 |
| pinyin-pro match | 3.29.5 | 4.77 | 6.91 |
| Fuse.js + Pinyin | 7.5.0 + 3.29.5 | 15.02 | 5.98 |
| Query | Expected | Text Search Engine | pinyin-pro match | Fuse.js + Pinyin |
|---|---|---|---|---|
jk github | React 监控平台 | React 监控平台 | React 监控平台 | React 监控平台 |
chongfu | 重复任务 | 重复任务 | 重复任务 | 重复任务, 银行服务 |
jvaascript | JavaScript | None | None | JavaScript |
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.
Real-world use and next steps
- The Blazwitcher release post explains how the extension uses this library to search tabs, bookmarks, and history.
- The library is listed in Ruanyifeng Weekly #314. This is a listing, not a performance endorsement.
- Try the multi-field demo or matching visualizer (both in Chinese), or see the full API and options.
Using a coding agent? The project includes an Agent Skill:
npx skills add https://github.com/cjinhuo/text-search-engine --skill text-search-engine