src/metrics/distance/levenshtein.js
function levenshteinForStrings(a, b) {
  if (a === b)
    return 0;

  const tmp = a;

  // Swapping the strings so that the shorter string is the first one.
  if (a.length > b.length) {
    a = b;
    b = tmp;
  }

  let la = a.length,
      lb = b.length;

  if (!la)
    return lb;
  if (!lb)
    return la;

  // Ignoring common suffix
  // NOTE: ~- is a fast - 1 operation, it does not work on big number though
  while (la > 0 && (a.charCodeAt(~-la) === b.charCodeAt(~-lb))) {
    la--;
    lb--;
  }

  if (!la)
    return lb;

  let start = 0;

  // Ignoring common prefix
  while (start < la && (a.charCodeAt(start) === b.charCodeAt(start)))
    start++;

  la -= start;
  lb -= start;

  if (!la)
    return lb;

  const v0 = VECTOR;

  let i = 0;

  while (i < lb) {
    CODES[i] = b.charCodeAt(start + i);
    v0[i] = ++i;
  }

  let current = 0,
      left,
      above,
      charA,
      j;

  // Starting the nested loops
  for (i = 0; i < la; i++) {
    left = i;
    current = i + 1;

    charA = a.charCodeAt(start + i);

    for (j = 0; j < lb; j++) {
      above = current;

      current = left;
      left = v0[j];

      if (charA !== CODES[j]) {

        // Insertion
        if (left < current)
          current = left;

        // Deletion
        if (above < current)
          current = above;

        current++;
      }

      v0[j] = current;
    }
  }

  return current;
}
function levenshteinForStrings(a, b) {
  if (a === b)
    return 0;

  const tmp = a;

  // Swapping the strings so that the shorter string is the first one.
  if (a.length > b.length) {
    a = b;
    b = tmp;
  }

  let la = a.length,
      lb = b.length;

  if (!la)
    return lb;
  if (!lb)
    return la;

  // Ignoring common suffix
  // NOTE: ~- is a fast - 1 operation, it does not work on big number though
  while (la > 0 && (a.charCodeAt(~-la) === b.charCodeAt(~-lb))) {
    la--;
    lb--;
  }

  if (!la)
    return lb;

  let start = 0;

  // Ignoring common prefix
  while (start < la && (a.charCodeAt(start) === b.charCodeAt(start)))
    start++;

  la -= start;
  lb -= start;

  if (!la)
    return lb;

  const v0 = VECTOR;

  let i = 0;

  while (i < lb) {
    CODES[i] = b.charCodeAt(start + i);
    v0[i] = ++i;
  }

  let current = 0,
      left,
      above,
      charA,
      j;

  // Starting the nested loops
  for (i = 0; i < la; i++) {
    left = i;
    current = i + 1;

    charA = a.charCodeAt(start + i);

    for (j = 0; j < lb; j++) {
      above = current;

      current = left;
      left = v0[j];

      if (charA !== CODES[j]) {

        // Insertion
        if (left < current)
          current = left;

        // Deletion
        if (above < current)
          current = above;

        current++;
      }

      v0[j] = current;
    }
  }

  return current;
}
function levenshteinForStrings ( a , b ) { if ( a === b ) return 0 ; const tmp = a ; // Swapping the strings so that the shorter string is the first one. if ( a . length > b . length ) { a = b ; b = tmp ; } let la = a . length , lb = b . length ; if ( ! la ) return lb ; if ( ! lb ) return la ; // Ignoring common suffix // NOTE: ~- is a fast - 1 operation, it does not work on big number though while ( la > 0 && ( a . charCodeAt ( ~ - la ) === b . charCodeAt ( ~ - lb ) ) ) { la -- ; lb -- ; } if ( ! la ) return lb ; let start = 0 ; // Ignoring common prefix while ( start < la && ( a . charCodeAt ( start ) === b . charCodeAt ( start ) ) ) start ++ ; la -= start ; lb -= start ; if ( ! la ) return lb ; const v0 = VECTOR ; let i = 0 ; while ( i < lb ) { CODES [ i ] = b . charCodeAt ( start + i ) ; v0 [ i ] = ++ i ; } let current = 0 , left , above , charA , j ; // Starting the nested loops for ( i = 0 ; i < la ; i ++ ) { left = i ; current = i + 1 ; charA = a . charCodeAt ( start + i ) ; for ( j = 0 ; j < lb ; j ++ ) { above = current ; current = left ; left = v0 [ j ] ; if ( charA !== CODES [ j ] ) { // Insertion if ( left < current ) current = left ; // Deletion if ( above < current ) current = above ; current ++ ; } v0 [ j ] = current ; } } return current ; }
Function returning the Levenshtein distance between two sequences. This
version only works on strings and leverage the `.charCodeAt` method to
perform fast comparisons between 16 bits integers.

@param  {string}  a - The first string to process.
@param  {string}  b - The second string to process.
@return {number}    - The Levenshtein distance between a & b.
Function returning the Levenshtein distance between two sequences . This version only works on strings and leverage the . charCodeAt method to perform fast comparisons between 16 bits integers .