string-natural-compare vs fuse.js vs string-similarity vs natural
String Matching and Similarity Comparison
1 Year
string-natural-comparefuse.jsstring-similaritynaturalSimilar Packages:
What's String Matching and Similarity?

String matching and similarity libraries in JavaScript provide tools for comparing strings to determine how alike they are. These libraries use various algorithms to calculate similarity scores, find matches, or perform fuzzy searching. They are useful in applications like search engines, data cleaning, and natural language processing. fuse.js is a powerful, lightweight fuzzy search library that allows for fast, client-side searching with customizable algorithms. natural is a comprehensive natural language processing library that includes string similarity, tokenization, and more, making it suitable for complex NLP tasks. string-natural-compare provides a simple way to compare strings using natural sorting algorithms, which is useful for ordering lists in a human-friendly manner. string-similarity is a lightweight library focused on calculating similarity scores between strings using various algorithms, ideal for applications that need precise similarity measurements.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
string-natural-compare4,426,72849-15 years agoMIT
fuse.js3,634,30718,845456 kB15a month agoApache-2.0
string-similarity2,575,0772,525-234 years agoISC
natural210,85310,71613.8 MB817 months agoMIT
Feature Comparison: string-natural-compare vs fuse.js vs string-similarity vs natural

Purpose and Functionality

  • string-natural-compare:

    string-natural-compare focuses on comparing strings using natural sorting algorithms. It is particularly useful for ordering strings in a way that aligns with human intuition, making it ideal for applications that need to display sorted lists.

  • fuse.js:

    fuse.js is designed for fuzzy searching, allowing users to find matches in a dataset even when the input is not an exact match. It is highly configurable, enabling developers to adjust the sensitivity of the search and specify which fields to search.

  • string-similarity:

    string-similarity specializes in calculating similarity scores between two strings using various algorithms. It is lightweight and efficient, making it suitable for applications that need to measure how alike two strings are for tasks like deduplication or matching.

  • natural:

    natural provides a wide range of natural language processing tools, including string similarity, tokenization, and stemming. It is more than just a string comparison library, making it suitable for projects that require comprehensive language processing capabilities.

Algorithm Complexity

  • string-natural-compare:

    string-natural-compare uses a natural comparison algorithm that is efficient for sorting and comparing strings. Its complexity is low, making it suitable for applications that need quick comparisons without heavy computational costs.

  • fuse.js:

    fuse.js uses a combination of fuzzy matching and scoring algorithms to find and rank matches. The complexity can vary based on the dataset size and the configuration of the search, but it is optimized for performance in client-side applications.

  • string-similarity:

    string-similarity offers multiple algorithms for calculating similarity scores, including Levenshtein distance and Jaro-Winkler. The library is designed to be efficient, with a focus on providing accurate similarity measurements without significant performance overhead.

  • natural:

    natural implements several string similarity algorithms, including Levenshtein distance, Jaro-Winkler, and more. The complexity of these algorithms varies, with some being more computationally intensive than others, but the library provides a good balance between accuracy and performance.

Customization

  • string-natural-compare:

    string-natural-compare is straightforward and does not offer much customization, as it is designed to perform a specific task: natural string comparison. It is best used as-is for its intended purpose.

  • fuse.js:

    fuse.js is highly customizable, allowing developers to configure the search algorithm, specify which fields to search, and adjust the sensitivity of the matching. This flexibility makes it suitable for a wide range of applications and datasets.

  • string-similarity:

    string-similarity allows for some customization in terms of the algorithms used to calculate similarity. Developers can choose which algorithm to use based on their needs, but the library is designed to be simple and easy to use without extensive configuration.

  • natural:

    natural provides some level of customization, particularly in terms of the algorithms used for string similarity. However, it is more focused on providing a comprehensive set of tools rather than being highly configurable for specific use cases.

Ease of Use: Code Examples

  • string-natural-compare:

    Natural comparison with string-natural-compare

    const naturalCompare = require('string-natural-compare');
    const arr = ['apple', 'banana', 'apple10', 'apple2'];
    arr.sort(naturalCompare);
    console.log(arr); // Sorted array: ['apple', 'apple10', 'apple2', 'banana']
    
  • fuse.js:

    Fuzzy search with fuse.js

    const Fuse = require('fuse.js');
    const list = [
      { name: 'Apple' },
      { name: 'Banana' },
      { name: 'Orange' },
    ];
    const options = { keys: ['name'] };
    const fuse = new Fuse(list, options);
    const result = fuse.search('Appl'); // Fuzzy search for 'Appl'
    console.log(result);
    
  • string-similarity:

    String similarity with string-similarity

    const stringSimilarity = require('string-similarity');
    const str1 = 'kitten';
    const str2 = 'sitting';
    const similarity = stringSimilarity.compareTwoStrings(str1, str2);
    console.log(similarity); // Similarity score between 0 and 1
    
  • natural:

    String similarity with natural

    const natural = require('natural');
    const string1 = 'Hello';
    const string2 = 'Hallo';
    const similarity = natural.JaroWinklerDistance(string1, string2);
    console.log(similarity); // Similarity score between 0 and 1
    
How to Choose: string-natural-compare vs fuse.js vs string-similarity vs natural
  • string-natural-compare:

    Use string-natural-compare when you need a simple and efficient way to compare strings using natural sorting. It is perfect for applications that require human-friendly ordering of strings without additional complexity.

  • fuse.js:

    Choose fuse.js if you need a fast, client-side fuzzy search solution with customizable matching algorithms. It is ideal for applications that require real-time search functionality with minimal setup.

  • string-similarity:

    Opt for string-similarity if you need a lightweight library focused on calculating similarity scores between strings. It is ideal for applications that require accurate similarity measurements without the overhead of a larger library.

  • natural:

    Select natural if you require a comprehensive suite of natural language processing tools, including advanced string similarity algorithms, tokenization, and language processing features. It is suitable for projects that need more than just string comparison.

README for string-natural-compare

String Natural Compare

NPM Version Build Status Coverage Status Dependencies Status

Compare alphanumeric strings the same way a human would, using a natural order algorithm (originally known as the alphanum algorithm) where numeric characters are sorted based on their numeric values rather than their ASCII values.

Standard sorting:   Natural order sorting:
    img1.png            img1.png
    img10.png           img2.png
    img12.png           img10.png
    img2.png            img12.png

This module exports a function that returns a number indicating whether one string should come before, after, or is the same as another string. It can be used directly with the native .sort() array method.

Fast and Robust

This module can compare strings containing any size of number and is heavily tested with a custom benchmark suite to make sure that it is as fast as possible.

Installation

npm install string-natural-compare --save
# or
yarn add string-natural-compare

Usage

naturalCompare(strA, strB[, options])

  • strA (string)
  • strB (string)
  • options (object) - Optional options object with the following options:
    • caseInsensitive (boolean) - Set to true to compare strings case-insensitively. Default: false.
    • alphabet (string) - A string of characters that define a custom character ordering. Default: undefined.
const naturalCompare = require('string-natural-compare');

// Simple, case-sensitive sorting
const files = ['z1.doc', 'z10.doc', 'z17.doc', 'z2.doc', 'z23.doc', 'z3.doc'];
files.sort(naturalCompare);
// -> ['z1.doc', 'z2.doc', 'z3.doc', 'z10.doc', 'z17.doc', 'z23.doc']


// Case-insensitive sorting
const chars = ['B', 'C', 'a', 'd'];
const naturalCompareCI = (a, b) => naturalCompare(a, b, {caseInsensitive: true});
chars.sort(naturalCompareCI);
// -> ['a', 'B', 'C', 'd']

// Note:
['a', 'A'].sort(naturalCompareCI); // -> ['a', 'A']
['A', 'a'].sort(naturalCompareCI); // -> ['A', 'a']


// Compare strings containing large numbers
naturalCompare(
  '1165874568735487968325787328996865',
  '265812277985321589735871687040841'
);
// -> 1
// (Other inputs with the same ordering as this example may yield a different number > 0)


// Sorting an array of objects
const hotelRooms = [
  {street: '350 5th Ave', room: 'A-1021'},
  {street: '350 5th Ave', room: 'A-21046-b'}
];
// Sort by street (case-insensitive), then by room (case-sensitive)
hotelRooms.sort((a, b) => (
  naturalCompare(a.street, b.street, {caseInsensitive: true}) ||
  naturalCompare(a.room, b.room)
));


// When text transformation is needed or when doing a case-insensitive sort on a
// large array of objects, it is best for performance to pre-compute the
// transformed text and store it on the object. This way, the text will not need
// to be transformed for every comparison while sorting.
const cars = [
  {make: 'Audi', model: 'R8'},
  {make: 'Porsche', model: '911 Turbo S'}
];
// Sort by make, then by model (both case-insensitive)
for (const car of cars) {
  car.sortKey = (car.make + ' ' + car.model).toLowerCase();
}
cars.sort((a, b) => naturalCompare(a.sortKey, b.sortKey));


// Using a custom alphabet (Russian alphabet)
const russianOpts = {
  alphabet: 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя',
};
['Ё', 'А', 'б', 'Б'].sort((a, b) => naturalCompare(a, b, russianOpts));
// -> ['А', 'Б', 'Ё', 'б']

Note: Putting numbers in the custom alphabet can cause undefined behaviour.