node-tfidf
Modern, zero-dependency TF-IDF implementation for Node.js
A beautifully simple, blazingly fast implementation of TF-IDF (Term Frequency-Inverse Document Frequency) for Node.js. Built with modern JavaScript, zero dependencies, and full TypeScript support.
โจ What's New in v0.1.0
This version is a complete rewrite with modern best practices:
- ๐ Zero Dependencies - No external packages, no security vulnerabilities
- โก 6.8x Faster - Intelligent IDF caching with proven benchmarks
- ๐ TypeScript Support - Full type definitions included
- โ
39 Comprehensive Tests - Every edge case covered
- ๐ฏ Modern API - Clean, intuitive methods with consistent naming
- ๐ Pluggable Tokenization - Language-agnostic core
- ๐ Backward Compatible - Legacy API still works
๐ฆ Installation
npm install node-tfidf
๐ Quick Start
const TfIdf = require('node-tfidf');
const tfidf = new TfIdf();
// Add documents
tfidf
.addDocument('This is a sample document about Node.js')
.addDocument('This is another document about Ruby')
.addDocument('Node.js is a JavaScript runtime');
// Calculate TF-IDF scores
const score = tfidf.score('Node.js', 0); // Score for document 0
const allScores = tfidf.scoreAll('Node.js'); // Scores for all documents
// Get top terms
const topTerms = tfidf.topTerms(0, 5); // Top 5 terms in document 0
console.log(topTerms);
// [
// { term: 'node', score: 2.2 },
// { term: 'js', score: 2.2 },
// { term: 'sample', score: 1.5 },
// ...
// ]
๐ API Reference
Constructor
const tfidf = new TfIdf(options);
Options:
tokenizer - Custom tokenizer object with tokenize(text) method
stopwords - Array of words to ignore
documents - Pre-existing documents for restoration
Example:
const tfidf = new TfIdf({
stopwords: ['the', 'a', 'an', 'is'],
tokenizer: customTokenizer
});
Methods
Document Management
addDocument(document, metadata)
Add a document to the corpus. Supports method chaining.
tfidf.addDocument('Sample text');
tfidf.addDocument(['pre', 'tokenized', 'array']);
tfidf.addDocument('Document with metadata', { _id: 'doc1', _category: 'tech' });
addDocuments(documents, metadataArray)
Add multiple documents at once (batch operation).
const docs = ['Doc one', 'Doc two', 'Doc three'];
const metadata = [{ _id: '1' }, { _id: '2' }, { _id: '3' }];
tfidf.addDocuments(docs, metadata);
addFileSync(path, encoding, metadata)
Add a document from a file (synchronous).
tfidf.addFileSync('./documents/article.txt', 'utf8', { _source: 'file' });
addFile(path, encoding, metadata)
Add a document from a file (asynchronous). Returns a Promise.
await tfidf.addFile('./documents/article.txt', 'utf8', { _source: 'file' });
// Parallel loading
await Promise.all([
tfidf.addFile('doc1.txt'),
tfidf.addFile('doc2.txt'),
tfidf.addFile('doc3.txt')
]);
removeDocument(documentIndex)
Remove a document from the corpus.
tfidf.removeDocument(2); // Remove document at index 2
updateDocument(documentIndex, document, metadata)
Update an existing document.
tfidf.updateDocument(0, 'New content', { _updated: true });
score(terms, documentIndex)
Calculate TF-IDF score for term(s) in a specific document.
const score = tfidf.score('machine learning', 0);
const multiScore = tfidf.score(['machine', 'learning'], 0);
scoreAll(terms)
Calculate TF-IDF scores for term(s) across all documents.
const scores = tfidf.scoreAll('javascript');
// [1.2, 0, 2.5, 0.8]
topTerms(documentIndex, limit)
Get top N terms for a document, sorted by TF-IDF score.
const top10 = tfidf.topTerms(0, 10);
// [{ term: 'machine', score: 3.5 }, { term: 'learning', score: 3.2 }, ...]
listTerms(documentIndex)
Get all terms for a document (sorted by TF-IDF score).
const allTerms = tfidf.listTerms(0);
setTokenizer(tokenizer)
Set a custom tokenizer.
const customTokenizer = {
tokenize: (text) => text.toLowerCase().split(/\s+/)
};
tfidf.setTokenizer(customTokenizer);
setStopwords(words) / addStopwords(words)
Set or add stopwords.
tfidf.setStopwords(['the', 'a', 'an']);
tfidf.addStopwords(['is', 'are', 'was']);
idf(term, force)
Calculate Inverse Document Frequency for a term. Uses caching unless force = true.
const idfScore = tfidf.idf('machine');
const recalculated = tfidf.idf('machine', true);
Properties
documentCount
Get the number of documents in the corpus.
console.log(tfidf.documentCount); // 3
Static Methods
TfIdf.tf(term, document)
Calculate raw term frequency.
const doc = { hello: 3, world: 1 };
const freq = TfIdf.tf('hello', doc); // 3
๐ Use Cases
Document Similarity
Find articles similar to the current one:
const similar = tfidf.findSimilar(currentArticleIndex, 5);
similar.forEach(({ index, similarity, metadata }) => {
console.log(`${metadata._title}: ${(similarity * 100).toFixed(1)}% similar`);
});
Search Engine
Build a simple but powerful search:
const results = tfidf.search(userQuery, 10, 0.3); // min score 0.3
results.forEach(({ index, score, metadata }) => {
console.log(`${metadata._title} (relevance: ${score.toFixed(2)})`);
});
Keyword Extraction
Extract important keywords from documents:
const keywords = tfidf.topTerms(docIndex, 10);
keywords.forEach(({ term, score }) => {
console.log(`${term}: ${score.toFixed(4)}`);
});
Duplicate Detection
Find near-duplicate documents:
for (let i = 0; i < tfidf.documentCount; i++) {
const similar = tfidf.findSimilar(i, 1);
if (similar.length > 0 && similar[0].similarity > 0.95) {
console.log(`Documents ${i} and ${similar[0].index} are duplicates`);
}
}
Content Recommendation
Recommend related content:
function recommend(currentDocIndex, numRecommendations = 5) {
return tfidf.findSimilar(currentDocIndex, numRecommendations)
.map(({ index, similarity, metadata }) => ({
title: metadata._title,
url: metadata._url,
relevance: similarity
}));
}
๐ฏ Advanced Usage
Custom Tokenization (Language-Specific)
// Vietnamese tokenizer example
const vietnameseTokenizer = {
tokenize: (text) => {
// Use your preferred Vietnamese tokenizer
return text.toLowerCase().split(/\s+/);
}
};
const tfidf = new TfIdf({ tokenizer: vietnameseTokenizer });
Document Metadata
tfidf.addDocument('Machine learning tutorial', {
_id: 'doc123',
_author: 'Jane Doe',
_category: 'AI'
});
// Metadata is preserved but not indexed
const data = tfidf.toJSON();
console.log(data.documents[0]._author); // 'Jane Doe'
Serialization
// Export
const data = tfidf.toJSON();
fs.writeFileSync('corpus.json', JSON.stringify(data));
// Restore
const restored = new TfIdf({ documents: data.documents });
Multi-Language Support
const englishStopwords = ['the', 'a', 'an', 'is', 'are', 'was', 'were'];
const spanishStopwords = ['el', 'la', 'de', 'que', 'y', 'es', 'en'];
const tfidf = new TfIdf({
stopwords: [...englishStopwords, ...spanishStopwords]
});
๐ Migration from v0.0.x
The new API is backward compatible, but we recommend updating to the modern API:
Old API (Still Works)
tfidf.tfidfs('term', function(i, measure, key) {
console.log('document #' + i + ' is ' + measure);
});
const score = tfidf.tfidf('term', 0);
New API (Recommended)
const scores = tfidf.scoreAll('term');
scores.forEach((score, i) => {
console.log(`document #${i} is ${score}`);
});
const score = tfidf.score('term', 0);
๐ Performance
Benchmarks on a modern laptop (Node.js v22):
| Operation |
Throughput |
Notes |
| IDF Lookup (cached) |
1.7M ops/sec |
6.8x faster than recalculation |
| Single Document Scoring |
991K ops/sec |
Very fast for real-time queries |
| Top Terms Extraction |
245K ops/sec |
Efficient even for large docs |
| Document Addition |
1.6K ops/sec |
Includes tokenization |
| Batch Add (100 docs) |
16K ops/sec |
10x faster than individual adds |
| Similarity Calculation |
500K ops/sec |
Cosine similarity between docs |
| Search (1000 docs) |
280 ops/sec |
Full corpus search with ranking |
Run benchmarks yourself:
npm run benchmark
๐ก Examples
Check out the examples/ directory for practical use cases:
# Run all examples
npm run examples
# Or run individually
node examples/01-basic-usage.js
node examples/02-document-similarity.js
node examples/03-search-engine.js
node examples/04-advanced-features.js
node examples/05-async-file-loading.js
Each example includes detailed comments and demonstrates real-world scenarios.
โ
Testing
# Modern test suite (39 tests)
npm test
# Legacy compatibility tests
npm run test:legacy
# Run all tests
npm run test:all
๐ What is TF-IDF?
TF-IDF (Term Frequency-Inverse Document Frequency) measures how important a word is to a document in a corpus.
- TF (Term Frequency): How often a term appears in a document
- IDF (Inverse Document Frequency): How rare a term is across all documents
- TF-IDF = TF ร IDF: High score means term is important to that specific document
Use Cases:
- Document similarity and ranking
- Keyword extraction
- Search relevance scoring
- Text classification
- Content recommendation
Formula:
TF-IDF(t, d) = TF(t, d) ร IDF(t)
IDF(t) = 1 + log(N / (1 + df))
Where:
t = term
d = document
N = total documents
df = document frequency (documents containing term)
๐ค Contributing
We love contributions! Here's how:
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature
- Add tests for your changes
- Ensure tests pass:
npm test
- Commit with clear messages:
git commit -m 'Add amazing feature'
- Push to your fork:
git push origin feature/amazing-feature
- Open a Pull Request
Development
# Run tests
npm test
# Run benchmarks
npm run benchmark
# Test backward compatibility
npm run test:legacy
๐ License
MIT License
Copyright (c) 2015-2025 Van-Duyet Le
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Made with โค๏ธ by Van-Duyet Le
If you find this library useful, please โญ star the repository!