<commit_before>const checkboardCache = {}

export const render = (c1, c2, size, serverCanvas) => {
  if (typeof document === 'undefined' && !serverCanvas) {
    return null
  }
  const canvas = serverCanvas ? new serverCanvas() : document.createElement('canvas')
  canvas.width = size * 2
  canvas.height = size * 2
  const ctx = canvas.getContext('2d')
  if (!ctx) {
    return null
  } // If no context can be found, return early.
  ctx.fillStyle = c1
  ctx.fillRect(0, 0, canvas.width, canvas.height)
  ctx.fillStyle = c2
  ctx.fillRect(0, 0, size, size)
  ctx.translate(size, size)
  ctx.fillRect(0, 0, size, size)
  return canvas.toDataURL()
}

export const get = (c1, c2, size, serverCanvas) => {
  const key = `${ c1 }-${ c2 }-${ size }${ serverCanvas ? '-server' : '' }`
  const checkboard = render(c1, c2, size, serverCanvas)

  if (checkboardCache[key]) {
    return checkboardCache[key]
  }
  checkboardCache[key] = checkboard
  return checkboard
}
<commit_msg>Stop creating new canvas on every checkboard get PATCH (#546)<commit_after>const checkboardCache = {}

export const render = (c1, c2, size, serverCanvas) => {
  if (typeof document === 'undefined' && !serverCanvas) {
    return null
  }
  const canvas = serverCanvas ? new serverCanvas() : document.createElement('canvas')
  canvas.width = size * 2
  canvas.height = size * 2
  const ctx = canvas.getContext('2d')
  if (!ctx) {
    return null
  } // If no context can be found, return early.
  ctx.fillStyle = c1
  ctx.fillRect(0, 0, canvas.width, canvas.height)
  ctx.fillStyle = c2
  ctx.fillRect(0, 0, size, size)
  ctx.translate(size, size)
  ctx.fillRect(0, 0, size, size)
  return canvas.toDataURL()
}

export const get = (c1, c2, size, serverCanvas) => {
  const key = `${ c1 }-${ c2 }-${ size }${ serverCanvas ? '-server' : '' }`

  if (checkboardCache[key]) {
    return checkboardCache[key]
  }

  const checkboard = render(c1, c2, size, serverCanvas)
  checkboardCache[key] = checkboard
  return checkboard
}

{'fraction_non_alphanumeric': 0.1430005017561465, 'fraction_numerical': 0.020572002007024585, 'mean_word_length': 3.691764705882353, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4119312', 'n_tokens_mistral': 688, 'n_tokens_neox': 674, 'n_words': 191}