quote replacing

Benchmark created on


Setup

const QUOTE_REGEX = /"/
const QUOTE2_REGEX = /"/g

function escapeAttribute(value) {
  // Handle non string values
  if (typeof value !== 'string') {
    // HTML Dates can just be ISO stringified
    if (value instanceof Date) {
      return value.toISOString()
    }

    // Calls toString() on the value
    value = String(value)
  }

  // This is a optimization to avoid the whole escaping process when the value
  // does not contain any double quotes
  if (!QUOTE_REGEX.test(value)) {
    return value
  }

  const length = value.length
  let escaped = ''

  let start = 0
  let end = 0

  for (; end < length; end++) {
    switch (value[end]) {
      case '"':
        escaped += value.slice(start, end) + '&#34;'
        start = end + 1
        continue
    }
  }

  // Appends the remaining string.
  escaped += value.slice(start, end)

  return escaped
}

function escapeAttribute2(value) {
  // Handle non string values
  if (typeof value !== 'string') {
    // HTML Dates can just be ISO stringified
    if (value instanceof Date) {
      return value.toISOString()
    }

    // Calls toString() on the value
    value = String(value)
  }

  return value.replace(QUOTE2_REGEX, '&#34;')
}

function escapeAttribute3(value) {
  // Handle non string values
  if (typeof value !== 'string') {
    // HTML Dates can just be ISO stringified
    if (value instanceof Date) {
      return value.toISOString()
    }

    // Calls toString() on the value
    value = String(value)
  }

  const length = value.length
  let escaped = ''

  let start = 0
  let end = 0

  for (; end < length; end++) {
    switch (value[end]) {
      case '"':
        escaped += value.slice(start, end) + '&#34;'
        start = end + 1
        continue
    }
  }

  // Appends the remaining string.
  escaped += value.slice(start, end)

  return escaped
}

Test runner

Ready to run.

Testing in
TestOps/sec
1
escapeAttribute("aaaaa\"aaa\"aaa")
escapeAttribute(" \"\"")
escapeAttribute("ajksdgh aiug\"od sdfhkg ashldfg askf asjkfgfgfgfgfgfgf\"gfgfgfgfgfgajkgasjkfg a\"ksdjfg  agkshdfa dfhasgk ")

escapeAttribute("aaaaa\"aaaaaa")
escapeAttribute(" \"")
escapeAttribute("ajksdgh aiug\"od sdfhkg ashldfg askf asjkfgfgfgfgfgfgfgfgfgfgfgfgajkgasjkfg aksdjfg  agkshdfa dfhasgk ")

escapeAttribute("aaaaaaaaaaa")
escapeAttribute(" ")
escapeAttribute("ajksdgh aiugod sdfhkg ashldfg askf asjkfgfgfgfgfgfgfgfgfgfgfgfgajkgasjkfg aksdjfg  agkshdfa dfhasgk ")
ready
2
escapeAttribute2("aaaaa\"aaa\"aaa")
escapeAttribute2(" \"\"")
escapeAttribute2("ajksdgh aiug\"od sdfhkg ashldfg askf asjkfgfgfgfgfgfgf\"gfgfgfgfgfgajkgasjkfg a\"ksdjfg  agkshdfa dfhasgk ")

escapeAttribute2("aaaaa\"aaaaaa")
escapeAttribute2(" \"")
escapeAttribute2("ajksdgh aiug\"od sdfhkg ashldfg askf asjkfgfgfgfgfgfgfgfgfgfgfgfgajkgasjkfg aksdjfg  agkshdfa dfhasgk ")

escapeAttribute2("aaaaaaaaaaa")
escapeAttribute2(" ")
escapeAttribute2("ajksdgh aiugod sdfhkg ashldfg askf asjkfgfgfgfgfgfgfgfgfgfgfgfgajkgasjkfg aksdjfg  agkshdfa dfhasgk ")
ready
3
escapeAttribute3("aaaaa\"aaa\"aaa")
escapeAttribute3(" \"\"")
escapeAttribute3("ajksdgh aiug\"od sdfhkg ashldfg askf asjkfgfgfgfgfgfgf\"gfgfgfgfgfgajkgasjkfg a\"ksdjfg  agkshdfa dfhasgk ")

escapeAttribute3("aaaaa\"aaaaaa")
escapeAttribute3(" \"")
escapeAttribute3("ajksdgh aiug\"od sdfhkg ashldfg askf asjkfgfgfgfgfgfgfgfgfgfgfgfgajkgasjkfg aksdjfg  agkshdfa dfhasgk ")

escapeAttribute3("aaaaaaaaaaa")
escapeAttribute3(" ")
escapeAttribute3("ajksdgh aiugod sdfhkg ashldfg askf asjkfgfgfgfgfgfgfgfgfgfgfgfgajkgasjkfg aksdjfg  agkshdfa dfhasgk ")
ready

Revisions

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