High-performance and memory efficient native C++ text similarity for Node.js
3 months ago
5
High-performance and memory efficient native C++ text similarity algorithms for Node.js with full Unicode support. text-similarity-node provides a suite of production-ready algorithms that demonstrably outperform pure JavaScript alternatives, especially in memory usage and specific use cases. This library is the best choice for comparing large documents where other JavaScript libraries slow down.
High Performance: Native C++ implementation which is fast and efficient compared to pure JavaScript libraries
Memory Efficient: Optimized for low memory usage and high throughput
Asynchronous API: Non-blocking operations using worker threads
Unicode Support: Full UTF-8 support including emoji and international characters
Multiple Algorithms: 7+ algorithms for different similarity needs
Production Ready: Memory safety, comprehensive testing, and error handling
Easy Integration: Simple API compatible with existing workflows
The text-similarity-node library was created based on algorithm implementations from the TextDistance Python library, achieving a 95% success rate for result compatibility between this library and the reference Python version. The 95% compatibility rate is due to different tokenization methods implemented for cosine similarity calculations.
Levenshtein Distance: Classic edit distance for spell checking and typo detection
Hamming Distance: Fixed-length string comparison for error detection
Jaro Similarity: Optimized for short strings and proper names
Jaro-Winkler: Enhanced Jaro with prefix matching bonus
Jaccard Similarity: Set intersection for document similarity
Sorensen-Dice: Harmonic mean of precision and recall
All algorithms support async execution with worker threads:
// All similarity algorithms available in async formawaittextSimilarity.async.levenshtein(s1,s2,caseSensitive)awaittextSimilarity.async.jaccard(s1,s2,useWords,caseSensitive,ngramSize)awaittextSimilarity.async.cosine(s1,s2,useWords,caseSensitive,ngramSize)awaittextSimilarity.async.jaro(s1,s2,caseSensitive)awaittextSimilarity.async.jaroWinkler(s1,s2,caseSensitive,prefixWeight)// ... and more
Based on extensive benchmarks, text-similarity-node stands out by delivering exceptional performance and scalability where it matters most.
Unmatched Memory Efficiency
Built with a native C++ core, text-similarity-node delivers a minimal memory footprint—ideal for memory-sensitive applications and large-scale data processing.
Jaccard Similarity: Uses just 392 bytes of heap memory, compared to over 35 KB for competitors like string-comparison (nearly 90× more).
Dice Coefficient: Allocates only 392 bytes, while alternatives require over 3 KB.
Exceptional Performance on Long Texts
text-similarity-node is optimized for long strings, outperforming JavaScript-based libraries:
For strings 70+ characters, it's nearly 6× faster than the popular similarity library.
For very long strings (1000+ characters), it's over 1000× faster, processing hundreds of thousands of operations per second while alternatives slow dramatically.
Dominant Speed in Key Algorithms
The library leads in performance for modern similarity use cases:
Jaccard Similarity: Over 5× faster than string-comparison — ideal for tag or keyword analysis.
Flexible Analysis Modes: Built-in character and word modes for Jaccard, Cosine, and Dice algorithms provide greater control over results.
Comprehensive Unicode support with proper handling of:
International Characters: Latin, Cyrillic, Greek, Chinese, Japanese, Arabic
This library was created using a reference implementation TextDistance Python library, which provided a solid foundation for the algorithms and features included in this library.