Sort Pieces

JavaScript performance comparison

Test case created by jfriend00

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const string1 = 'AYUKB17053UI903TBC';
  const string2  = 'ABKUY01357IU039BCT';
  let result;
  

};

Benchmark.prototype.teardown = function() {
  if (result !== string2) {
      throw new Error(`invalid result "${result}"`);
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Single result array
function sortPieces(str) {
    const result = [];
    let lastPiece = [];
    let lastType;
    for (const char of str) {
        const nextType = (char >= "0" && char <= "9") ? "number" : "letter";
        if (nextType === lastType || !lastType) {
            // either same type as previous char or first char in string
            lastPiece.push(char);
        } else {
            // different type of char than previous char, start a new piece
            result.push(...lastPiece.sort());
            lastPiece = [char];
        }
        lastType = nextType;
    }
    result.push(...lastPiece.sort());
    return result.join("");    
}

result = sortPieces(string1);
pending…
Array of arrays
function sortPieces(str) {
    const piecesArray = [];
    let lastPiece = [];
    let lastType;
    for (const char of str) {
        const nextType = (char >= "0" && char <= "9") ? "number" : "letter";
        if (nextType === lastType || !lastType) {
            // either same type as previous char or first char in string
            lastPiece.push(char);
        } else {
            // different type of char than previous char, start a new piece
            piecesArray.push(lastPiece.sort());
            lastPiece = [char];
        }
        lastType = nextType;
    }
    piecesArray.push(lastPiece.sort());
    return piecesArray.flat(1).join("");    
}

result = sortPieces(string1);
pending…
Original
function fn1(string1) {
      let tempArray = [[]]
      for (const char of string1) {
        let lastCharIsNumber
        let currentCharIsNumber
        const lastArray = tempArray[tempArray.length - 1]
        if(!lastArray.length){
          lastArray.push(char)
          continue
        }
        currentCharIsNumber = !Number.isNaN(Number(char))
        lastCharIsNumber = !Number.isNaN(Number(lastArray[lastArray.length - 1]))
        if (currentCharIsNumber && lastCharIsNumber)  lastArray.push(char)
        else if (!currentCharIsNumber && !lastCharIsNumber) lastArray.push(char)
        else tempArray.push([char])
      }
    
      tempArray.forEach(item => item.sort())
    
      return tempArray.map(array => array.join('')).join('')
    }

    result = fn1(string1);
pending…
Experiment
// Make nextType be number comparison instead of string comparison
// Try isNaN() for determining if char is a digit
// Try moving nextType to higher scope so it can be reused
// Try moving char to higher scope so it can be resued
// Try splitting the array first rather than iterating it
// Try mapping array to charCodes

function sortPieces(str) {
    const result = [];
    let lastPiece = [];
    let lastType, nextType;
    for (const char of str) {
        nextType = (char >= "0" && char <= "9") ? 1 : 2;
        if (nextType === lastType || !lastType) {
            // either same type as previous char or first char in string
            lastPiece.push(char);
        } else {
            // different type of char than previous char, start a new piece
            result.push(...lastPiece.sort());
            lastPiece = [char];
        }
        lastType = nextType;
    }
    result.push(...lastPiece.sort());
    return result.join("");    
}


result = sortPieces(string1);
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments