Skip to content

Fastest JavaScript implementation of the porter2 stemming algorithm

License

Notifications You must be signed in to change notification settings

eilvelia/porter2.js

Repository files navigation

porter2   npm CI

Fast JavaScript implementation of the porter2 English stemming algorithm.

$ npm install porter2

Usage

The package is simple: it has no dependencies and exports a single function named stem.

Import using CommonJS:

const { stem } = require('porter2')

Or, import using EcmaScript Modules (through interopability with CommonJS):

import { stem } from 'porter2'

Use the stemmer:

const word = stem('animadversion')
console.log(word) //=> animadvers

This stemmer expects a lowercase English word.

The code is compatible with ES5. TypeScript type declarations are included.

Benchmarks

On my machine, the 29.4k test suite executes in ~5.25ms (~5.6M/s throughput) in a hot loop (~31ms for the first run).

Here is a comparison with some other libraries (you probably should take it with a little grain of salt):

library throughput (node) throughput (bun)
porter2 1.0.3 5621 kops/s 6780 kops/s
stemr 1.0.0 840 kops/s 920 kops/s
wink-porter2-stemmer 1 2.0.1 340 kops/s 420 kops/s

Here are libraries that implement older porter version 1 (note the behavior is not identical):

library throughput (node) throughput (bun)
porter-stemmer-js 2 1.1.2 3280 kops/s 3373 kops/s
stemmer 3 2.0.1 2100 kops/s 1549 kops/s
@stdlib/nlp-porter-stemmer 0.2.2 1712 kops/s 1604 kops/s
porter-stemmer 0.9.1 951 kops/s 1264 kops/s

The benchmark code is in bench/run.mjs. This is tested with Node.js v22.14.0 and bun v1.2.10 on Zen 3 (4.5 GHz boosted). The library versions are latest as of 2025-04-29.

Footnotes

  1. wink-porter2-stemmer is 99.97% porter2 compliant (fails on ' cases only)

  2. That one has similar goals and, surprisingly, was published just 3 days before this package! (And after the author started working on porter2.js.)

  3. ESM only

About

Fastest JavaScript implementation of the porter2 stemming algorithm

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published