Hexchess stringify

Benchmark created on


Setup

const positions = [
  'f11',
  'e10',
  'f10',
  'g10',
  'd9',
  'e9',
  'f9',
  'g9',
  'h9',
  'c8',
  'd8',
  'e8',
  'f8',
  'g8',
  'h8',
  'i8',
  'b7',
  'c7',
  'd7',
  'e7',
  'f7',
  'g7',
  'h7',
  'i7',
  'k7',
  'a6',
  'b6',
  'c6',
  'd6',
  'e6',
  'f6',
  'g6',
  'h6',
  'i6',
  'k6',
  'l6',
  'a5',
  'b5',
  'c5',
  'd5',
  'e5',
  'f5',
  'g5',
  'h5',
  'i5',
  'k5',
  'l5',
  'a4',
  'b4',
  'c4',
  'd4',
  'e4',
  'f4',
  'g4',
  'h4',
  'i4',
  'k4',
  'l4',
  'a3',
  'b3',
  'c3',
  'd3',
  'e3',
  'f3',
  'g3',
  'h3',
  'i3',
  'k3',
  'l3',
  'a2',
  'b2',
  'c2',
  'd2',
  'e2',
  'f2',
  'g2',
  'h2',
  'i2',
  'k2',
  'l2',
  'a1',
  'b1',
  'c1',
  'd1',
  'e1',
  'f1',
  'g1',
  'h1',
  'i1',
  'k1',
  'l1',
]

function createBoard() {
  return {
    f11: 'b',
    e10: 'q',
    f10: 'b',
    g10: 'k',
    d9: 'n',
    e9: null,
    f9: 'b',
    g9: null,
    h9: 'n',
    c8: 'r',
    d8: null,
    e8: null,
    f8: null,
    g8: null,
    h8: null,
    i8: 'r',
    b7: 'p',
    c7: 'p',
    d7: 'p',
    e7: 'p',
    f7: 'p',
    g7: 'p',
    h7: 'p',
    i7: 'p',
    k7: 'p',
    a6: null,
    b6: null,
    c6: null,
    d6: null,
    e6: null,
    f6: null,
    g6: null,
    h6: null,
    i6: null,
    k6: null,
    l6: null,
    a5: null,
    b5: null,
    c5: null,
    d5: null,
    e5: null,
    f5: 'P',
    g5: null,
    h5: null,
    i5: null,
    k5: null,
    l5: null,
    a4: null,
    b4: null,
    c4: null,
    d4: null,
    e4: 'P',
    f4: null,
    g4: 'P',
    h4: null,
    i4: null,
    k4: null,
    l4: null,
    a3: null,
    b3: null,
    c3: null,
    d3: 'P',
    e3: null,
    f3: 'B',
    g3: null,
    h3: 'P',
    i3: null,
    k3: null,
    l3: null,
    a2: null,
    b2: null,
    c2: 'P',
    d2: null,
    e2: null,
    f2: 'B',
    g2: null,
    h2: null,
    i2: 'P',
    k2: null,
    l2: null,
    a1: null,
    b1: 'P',
    c1: 'R',
    d1: 'N',
    e1: 'Q',
    f1: 'B',
    g1: 'K',
    h1: 'N',
    i1: 'R',
    k1: 'P',
    l1: null
  }
}

Test runner

Ready to run.

Testing in
TestOps/sec
1.2.2
function stringifyBoard() {
  const board = createBoard()

  return positions
    .map((position, index) => {
      const char = board[position] || '_'

      return (
        index === 0 ||
        index === 3 ||
        index === 8 ||
        index === 15 ||
        index === 24 ||
        index === 35 ||
        index === 46 ||
        index === 57 ||
        index === 68 ||
        index === 79
      ) ? `${char}/` : char
    })
    .join('')
    .replaceAll('___________', '11')
    .replaceAll('__________', '10')
    .replaceAll('_________', '9')
    .replaceAll('________', '8')
    .replaceAll('_______', '7')
    .replaceAll('______', '6')
    .replaceAll('_____', '5')
    .replaceAll('____', '4')
    .replaceAll('___', '3')
    .replaceAll('__', '2')
    .replaceAll('_', '1')
}

stringifyBoard()
ready
Alternate 1
function stringifyBoard() {
  const board = createBoard()

  let fen = ''
  let char = ''
  let count = 0

  for (let i = 0; i < positions.length; i++) {
    char = board[positions[i]] ?? '_'

    if (char === '_') {
      count++
    } else {
      if (count) {
        fen += count.toString()
      }

      count = 0
      fen += char
    }

    if (
      i === 0 ||
      i === 3 ||
      i === 8 ||
      i === 15 ||
      i === 24 ||
      i === 35 ||
      i === 46 ||
      i === 57 ||
      i === 68 ||
      i === 79
    ) {
      if (count) {
        fen += count.toString()
      }

      count = 0
      fen += '/'
    }
  }

  if (count) {
    fen += count.toString()
  }

  return fen
}

stringifyBoard()
ready
Alternate 2
function stringifyBoard() {
  const board = createBoard()

  let blank = 0
  let current
  let output = ''

  for (let i = 0; i < positions.length; i++) {
    current = board[positions[i]]

    if (current) {
      if (blank) {
        output += blank
        blank = 0
      }

      output += current
    } else {
      blank++
    }

    if (
      i === 0 ||
      i === 3 ||
      i === 8 ||
      i === 15 ||
      i === 24 ||
      i === 35 ||
      i === 46 ||
      i === 57 ||
      i === 68 ||
      i === 79
    ) {
      output += (blank || '') + '/'
      blank = 0
    }
  }

  if (blank) {
    output += blank
  }

  return output
}

stringifyBoard()
ready

Revisions

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