jieba-rs binding to Node.js
node-rs/jieba was prebuilt into binary already, so you don't need fighting with node-gyp and c++ toolchain.
Due to jieba-rs is 33% faster than cppjieba, and N-API is faster than v8 C++ API, @node-rs/jieba is faster than nodejieba.
Benchmarked with jieba-rs@0.11.0 vs nodejieba@3.5.8 on Apple M4 Max, Node.js v26.8.2:
Benchmark Cut 1184 words result
┌─────────┬──────────────────┬──────────────────┬───────────────────┬────────────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │
├─────────┼──────────────────┼──────────────────┼───────────────────┼────────────────────────┼────────────────────────┼─────────┤
│ 0 │ '@node-rs/jieba' │ '61311 ± 0.08%' │ '61041 ± 416.00' │ '16342 ± 0.06%' │ '16382 ± 111' │ 16311 │
│ 1 │ 'nodejieba' │ '133632 ± 0.11%' │ '133875 ± 1667.0' │ '7497 ± 0.09%' │ '7470 ± 92' │ 7484 │
└─────────┴──────────────────┴──────────────────┴───────────────────┴────────────────────────┴────────────────────────┴─────────┘
Benchmark Cut 246568 words result
┌─────────┬──────────────────┬────────────────────┬─────────────────────┬────────────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │
├─────────┼──────────────────┼────────────────────┼─────────────────────┼────────────────────────┼────────────────────────┼─────────┤
│ 0 │ '@node-rs/jieba' │ '17941189 ± 1.06%' │ '17739146 ± 215874' │ '56 ± 0.98%' │ '56 ± 1' │ 64 │
│ 1 │ 'nodejieba' │ '39167640 ± 1.22%' │ '38399145 ± 888271' │ '26 ± 1.15%' │ '26 ± 1' │ 64 │
└─────────┴──────────────────┴────────────────────┴─────────────────────┴────────────────────────┴────────────────────────┴─────────┘
Benchmark Tag 1184 words result
┌─────────┬──────────────────┬──────────────────┬───────────────────┬────────────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │
├─────────┼──────────────────┼──────────────────┼───────────────────┼────────────────────────┼────────────────────────┼─────────┤
│ 0 │ '@node-rs/jieba' │ '158515 ± 0.09%' │ '157416 ± 1249.0' │ '6315 ± 0.07%' │ '6353 ± 51' │ 6309 │
│ 1 │ 'nodejieba' │ '305060 ± 0.12%' │ '303375 ± 2541.0' │ '3281 ± 0.10%' │ '3296 ± 27' │ 3279 │
└─────────┴──────────────────┴──────────────────┴───────────────────┴────────────────────────┴────────────────────────┴─────────┘
Benchmark Tag 246568 words result
┌─────────┬──────────────────┬────────────────────┬──────────────────────┬────────────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │
├─────────┼──────────────────┼────────────────────┼──────────────────────┼────────────────────────┼────────────────────────┼─────────┤
│ 0 │ '@node-rs/jieba' │ '42167642 ± 1.49%' │ '41271917 ± 769729' │ '24 ± 1.36%' │ '24 ± 0' │ 64 │
│ 1 │ 'nodejieba' │ '86278741 ± 0.86%' │ '85348500 ± 1364291' │ '12 ± 0.83%' │ '12 ± 0' │ 64 │
└─────────┴──────────────────┴────────────────────┴──────────────────────┴────────────────────────┴────────────────────────┴─────────┘import { Jieba } from '@node-rs/jieba'
import { dict } from '@node-rs/jieba/dict'
// load jieba with the default dict
const jieba = Jieba.withDict(dict)
console.info(jieba.cut('我们中出了一个叛徒', false))
// ["我们", "中", "出", "了", "一个", "叛徒"]import { Jieba, TfIdf } from '@node-rs/jieba'
import { dict, idf } from '@node-rs/jieba/dict'
const jieba = Jieba.withDict(dict)
const tfIdf = TfIdf.withDict(idf)
tfIdf.extractKeywords(
jieba,
'今天纽约的天气真好啊,京华大酒店的张尧经理吃了一只北京烤鸭。后天纽约的天气不好,昨天纽约的天气也不好,北京烤鸭真好吃',
3,
)
// [
// { keyword: '北京烤鸭', weight: 1.3904870323222223 },
// { keyword: '纽约', weight: 1.121759684755 },
// { keyword: '天气', weight: 1.0766573240983333 }
// ]import { Jieba } from '@node-rs/jieba'
const customDict = ['哪行 50', '干一行 51', '行一行 52', '行行 53']
const dictBuffer = Buffer.from(customDict.join('\n'), 'utf-8')
const jieba = Jieba.withDict(dictBuffer)
const text = '人要是行干一行行一行,一行行行行行,行行行干哪行都行'
const output = jieba.cut(text, false)
console.log('分词结果⤵️\n', output.join('/'))
// Before: 人/要是/行/干/一行行/一行/,/一行行/行/行/行/,/行/行/行/干/哪/行/都行
// After: 人/要是/行/干一行/行一行/,/一行行/行行/行/,/行行/行/干/哪行/都行
// Pinyin: rén yào shi xíng gàn yì háng xíng yì háng , yì háng xíng háng háng xíng , háng háng xíng gàn nǎ háng dōu xíng