Board normalizing (v2)

Revision 2 of this benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
String replace
function parseBoard(source) {
  const normalized = source
    .replaceAll('11', '___________')
    .replaceAll('10', '__________')
    .replaceAll('9', '_________')
    .replaceAll('8', '________')
    .replaceAll('7', '_______')
    .replaceAll('6', '______')
    .replaceAll('5', '_____')
    .replaceAll('4', '____')
    .replaceAll('3', '___')
    .replaceAll('2', '__')
    .replaceAll('1', '_')
    .replaceAll('/', '')
    
  return normalized
}

parseBoard('b/qbk/n1b1n/r5r/ppppppppp/11/5P5/4P1P4/3P1B1P3/2P2B2P2/1PRNQBKNRP1')
ready
Character iteration
function parseBoard(source) {
  let normalized = ''

  for (let i = 0; i < source.length; i++) {
    switch (source[i]) {
      case '1':
        switch (source[i + 1]) {
          case '0':
          	i++
            normalized += '__________';
            continue
          case '1':
            i++
            normalized += '___________';
            continue
          default:
            normalized += '_';
            continue
        }
      case '2':
        normalized += '__';
        continue
      case '3':
        normalized += '___';
        continue
      case '4':
        normalized += '____';
        continue
      case '5':
        normalized += '_____';
        continue
      case '6':
        normalized += '______';
        continue
      case '7':
        normalized += '_______';
        continue
      case '8':
        normalized += '________';
        continue
      case '9':
        normalized += '_________';
        continue
      case '/':
      	continue
      default:
        normalized += source[i]
    }
  }

  return normalized
}

parseBoard('b/qbk/n1b1n/r5r/ppppppppp/11/5P5/4P1P4/3P1B1P3/2P2B2P2/1PRNQBKNRP1')
ready

Revisions

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