A fast and lightweight spell checker library
npm install fast-spellA fast and lightweight spell checker library optimized for performance. This library provides efficient spell checking and suggestion capabilities with support for affix rules.
- โก High-performance spell checking (1M+ operations/sec)
- ๐ Smart word suggestions
- ๐ Support for affix rules (prefixes and suffixes)
- ๐พ Efficient memory usage with Trie-based dictionary
- ๐ Intelligent caching system
- ๐ Multiple loading methods: files, URLs (beta), and imports (beta)
- ๐ฆ Vite and modern bundler compatible
- ๐ฅ๏ธ Works in both Node.js and browser environments
- ๐ Thoroughly benchmarked
``bash`
npm install fast-spellor
bun install fast-spell
`typescript
import SpellChecker from "fast-spell";
// Initialize with dictionary and affix rules files
const spellChecker = new SpellChecker(
"path/to/dictionary.dic",
"path/to/rules.aff",
);
// Check if a word is spelled correctly
const isCorrect = spellChecker.check("hello"); // true
const isIncorrect = spellChecker.check("helllo"); // false
// Get spelling suggestions
const suggestions = spellChecker.suggest("helllo"); // ['hello', 'hell', ...]
`
> [!NOTE]
> URL and import loading is a beta feature. Please open an issue if you encounter any problems.
`typescript
import SpellChecker from "fast-spell";
// Load from URLs (great for CDNs)
const spellChecker = await SpellChecker.create(
"https://cdn.example.com/dictionaries/en_US-web.dic",
"https://cdn.example.com/dictionaries/en_US-web.aff",
);
// Load from imported content (perfect for Vite/Webpack)
import dictionaryContent from "./dictionaries/en_US-web.dic?raw";
import affixContent from "./dictionaries/en_US-web.aff?raw";
const spellChecker = await SpellChecker.create(
() => dictionaryContent,
() => affixContent,
);
// Load dynamically
const spellChecker = await SpellChecker.create(
async () => await fetch("/api/dictionary").then((r) => r.text()),
async () => await fetch("/api/affix-rules").then((r) => r.text()),
);
`
`typescript
import SpellChecker from "fast-spell";
const spellChecker = new SpellChecker(); // Empty initially
// Load dictionaries later
await spellChecker.loadDictionaries(
"https://example.com/dictionary.dic",
() => myAffixRulesContent,
);
`
fast-spell supports multiple ways to load dictionaries and affix rules:
| Source Type | Example | Use Case |
| ------------------ | ---------------------------------------- | ------------------------------------- |
| File Path | "./dict.dic" | Node.js applications |"https://cdn.com/dict.dic"
| URL String | | Loading from CDNs or APIs |new URL("./dict.dic", import.meta.url)
| URL Object | | Relative URLs in modules |() => fileContent
| Sync Function | | Bundled content (Vite ?raw imports) |async () => await fetch(...)
| Async Function | | Dynamic loading |
The GitHub repository contains dictionaries and affix rules for various languages.
- English Dictionary
- English Affix Rules
You can use these files in several ways:
Download the files and reference them by path:
`typescript`
const spellChecker = new SpellChecker(
"./data/en_US-web.dic",
"./data/en_US-web.aff",
);
Import the content directly:
`typescript
import dictContent from "./data/en_US-web.dic?raw";
import affixContent from "./data/en_US-web.aff?raw";
const spellChecker = await SpellChecker.create(
() => dictContent,
() => affixContent,
);
`
Reference them directly from GitHub or your own CDN:
`typescript`
const spellChecker = await SpellChecker.create(
"https://raw.githubusercontent.com/The-Best-Codes/fast-spell/main/data/en_US-web.dic",
"https://raw.githubusercontent.com/The-Best-Codes/fast-spell/main/data/en_US-web.aff",
);
Based on our benchmark tests, fast-spell achieves impressive performance metrics:
- Dictionary load time: ~130ms
- Single word check: ~1ยตs
- Cached operations: 1M+ ops/sec
- Suggestion generation: ~630K ops/sec
- Memory usage: ~75MB heap for typical dictionary
| Operation | Time (Average) | Operations/sec |
| -------------------- | -------------- | -------------- |
| Cached Word Check | 920ns | 1,086,031 |
| Uncached Word Check | 1.07ยตs | 938,457 |
| Correct Word Check | 960ns | 1,041,043 |
| Incorrect Word Check | 832ns | 1,201,789 |
| Affixed Word Check | 866ns | 1,153,526 |
| Spell Suggestions | 1.10ยตs | 909,406 |
#### Constructor
`typescript`
constructor(dicPath?: string, affPath?: string)
- dicPath: Path to the dictionary file (optional, Node.js only)affPath
- : Path to the affix rules file (optional, Node.js only)
Creates a SpellChecker instance with synchronous file loading. Only works in Node.js environments.
#### Static Methods
##### SpellChecker.create(dicSource?, affSource?): Promise
`typescript`
static async create(dicSource?: DictionarySource, affSource?: DictionarySource): Promise
Creates a SpellChecker instance with asynchronous loading support. Works in all environments.
- dicSource: Dictionary source (file path, URL, or content function)affSource
- : Affix rules source (file path, URL, or content function)
#### Instance Methods
##### check(word: string): boolean
Checks if a word is spelled correctly.
##### suggest(word: string): string[]
Returns an array of spelling suggestions for a given word.
##### loadDictionaries(dicSource, affSource): Promise
`typescript`
async loadDictionaries(dicSource: DictionarySource, affSource: DictionarySource): Promise
Loads or reloads dictionary and affix rules. Clears existing data and caches.
`typescript`
type DictionarySource = string | URL | (() => Promise
Represents the different ways to provide dictionary content:
- string: File path (Node.js) or URL (all environments)URL
- : URL object for fetching content() => string
- : Synchronous function returning content() => Promise
- : Asynchronous function returning content
`bashInstall dependencies
bun install
MIT