function _boundedLevenshtein(a, b, tolerance) {
        // the strings are the same
        if (a === b) {
            return 0;
        }
        // a should be the shortest string
        const swap = a;
        if (a.length > b.length) {
            a = b;
            b = swap;
        }
        let lenA = a.length;
        let lenB = b.length;
        // ignore common suffix
        // note: `~-` decreases by a unit in a bitwise fashion
        while(lenA > 0 && a.charCodeAt(~-lenA) === b.charCodeAt(~-lenB)){
            lenA--;
            lenB--;
        }
        // early return when the smallest string is empty
        if (!lenA) {
            return lenB > tolerance ? -1 : lenB;
        }
        // ignore common prefix
        let startIdx = 0;
        while(startIdx < lenA && a.charCodeAt(startIdx) === b.charCodeAt(startIdx)){
            startIdx++;
        }
        lenA -= startIdx;
        lenB -= startIdx;
        // early return when the smallest string is empty
        if (lenA === 0) {
            return lenB > tolerance ? -1 : lenB;
        }
        const delta = lenB - lenA;
        if (tolerance > lenB) {
            tolerance = lenB;
        } else if (delta > tolerance) {
            return -1;
        }
        let i = 0;
        const row = [];
        const characterCodeCache = [];
        while(i < tolerance){
            characterCodeCache[i] = b.charCodeAt(startIdx + i);
            row[i] = ++i;
        }
        while(i < lenB){
            characterCodeCache[i] = b.charCodeAt(startIdx + i);
            row[i++] = tolerance + 1;
        }
        const offset = tolerance - delta;
        const haveMax = tolerance < lenB;
        let jStart = 0;
        let jEnd = tolerance;
        let current = 0;
        let left = 0;
        let above = 0;
        let charA = 0;
        let j = 0;
        // Starting the nested loops
        for(i = 0; i < lenA; i++){
            left = i;
            current = i + 1;
            charA = a.charCodeAt(startIdx + i);
            jStart += i > offset ? 1 : 0;
            jEnd += jEnd < lenB ? 1 : 0;
            for(j = jStart; j < jEnd; j++){
                above = current;
                current = left;
                left = row[j];
                if (charA !== characterCodeCache[j]) {
                    // insert current
                    if (left < current) {
                        current = left;
                    }
                    // delete current
                    if (above < current) {
                        current = above;
                    }
                    current++;
                }
                row[j] = current;
            }
            if (haveMax && row[i + delta] > tolerance) {
                return -1;
            }
        }
        return current <= tolerance ? current : -1;
    }
Inspired by:
https://github.com/Yomguithereal/talisman/blob/86ae55cbd040ff021d05e282e0e6c71f2dde21f8/src/metrics/levenshtein.js#L218-L340