replce vs slice

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
replace
const removeAnchors = (regexString) => {
  return regexString.replace(/^\^|\$$/g, '')
}

const testCases = [
    { input: '^a^b$c$d$', expected: 'a^b$c$d' },
    { input: 'a^b$c$d$', expected: 'a^b$c$d' },
    { input: '^a^b$c$d', expected: 'a^b$c$d' },
    { input: 'a^b$c$d', expected: 'a^b$c$d' },
    { input: '^a^b$c$d$e$', expected: 'a^b$c$d$e' },
    { input: 'a^b$c$d$e$', expected: 'a^b$c$d$e' },
    { input: '^a^b$c$d$e', expected: 'a^b$c$d$e' },
    { input: 'a^b$c$d$e', expected: 'a^b$c$d$e' },
    { input: '^a^b$c$d$e$f$', expected: 'a^b$c$d$e$f' },
    { input: 'a^b$c$d$e$f$', expected: 'a^b$c$d$e$f' },
    { input: '^a^b$c$d$e$f', expected: 'a^b$c$d$e$f' },
    { input: 'a^b$c$d$e$f', expected: 'a^b$c$d$e$f' },
    { input: '^a^b$c$d$e$f$g$', expected: 'a^b$c$d$e$f$g' },
    { input: 'a^b$c$d$e$f$g$', expected: 'a^b$c$d$e$f$g' },
    { input: '^a^b$c$d$e$f$g', expected: 'a^b$c$d$e$f$g' },
    { input: 'a^b$c$d$e$f$g', expected: 'a^b$c$d$e$f$g' },
    { input: '^a^b$c$d$e$f$g$h$', expected: 'a^b$c$d$e$f$g$h' },
    { input: 'a^b$c$d$e$f$g$h$', expected: 'a^b$c$d$e$f$g$h' },
    { input: '^a^b$c$d$e$f$g$h', expected: 'a^b$c$d$e$f$g$h' },
    { input: 'a^b$c$d$e$f$g$h', expected: 'a^b$c$d$e$f$g$h' },
    { input: '^a^b$c$d$e$f$g$h$i$', expected: 'a^b$c$d$e$f$g$h$i' },
    { input: 'a^b$c$d$e$f$g$h$i$', expected: 'a^b$c$d$e$f$g$h$i' },
    { input: '^a^b$c$d$e$f$g$h$i', expected: 'a^b$c$d$e$f$g$h$i' },
    { input: 'a^b$c$d$e$f$g$h$i', expected: 'a^b$c$d$e$f$g$h$i' },
    { input: '^a^b$c$d$e$f$g$h$i$j$', expected: 'a^b$c$d$e$f$g$h$i$j' },
    { input: 'a^b$c$d$e$f$g$h$i$j$', expected: 'a^b$c$d$e$f$g$h$i$j' },
    { input: '^a^b$c$d$e$f$g$h$i$j', expected: 'a^b$c$d$e$f$g$h$i$j' },
    { input: 'a^b$c$d$e$f$g$h$i$j', expected: 'a^b$c$d$e$f$g$h$i$j' },
    { input: '^a^b$c$d$e$f$g$h$i$j$k$', expected: 'a^b$c$d$e$f$g$h$i$j$k' },
    { input: 'a^b$c$d$e$f$g$h$i$j$k$', expected: 'a^b$c$d$e$f$g$h$i$j$k' },
    { input: '^a^b$c$d$e$f$g$h$i$j$k', expected: 'a^b$c$d$e$f$g$h$i$j$k' },
    { input: 'a^b$c$d$e$f$g$h$i$j$k', expected: 'a^b$c$d$e$f$g$h$i$j$k' },
  ]

  testCases.forEach(({ input, expected }) => {
      const result = removeAnchors(input)
  })
ready
slice
const removeAnchors = (regexString) => {
      if (regexString.startsWith('^')) {
      regexString = regexString.slice(1)
    }
    if (regexString.endsWith('$')) {
      regexString = regexString.slice(0, -1)
    }
    return regexString
}

const testCases = [
    { input: '^a^b$c$d$', expected: 'a^b$c$d' },
    { input: 'a^b$c$d$', expected: 'a^b$c$d' },
    { input: '^a^b$c$d', expected: 'a^b$c$d' },
    { input: 'a^b$c$d', expected: 'a^b$c$d' },
    { input: '^a^b$c$d$e$', expected: 'a^b$c$d$e' },
    { input: 'a^b$c$d$e$', expected: 'a^b$c$d$e' },
    { input: '^a^b$c$d$e', expected: 'a^b$c$d$e' },
    { input: 'a^b$c$d$e', expected: 'a^b$c$d$e' },
    { input: '^a^b$c$d$e$f$', expected: 'a^b$c$d$e$f' },
    { input: 'a^b$c$d$e$f$', expected: 'a^b$c$d$e$f' },
    { input: '^a^b$c$d$e$f', expected: 'a^b$c$d$e$f' },
    { input: 'a^b$c$d$e$f', expected: 'a^b$c$d$e$f' },
    { input: '^a^b$c$d$e$f$g$', expected: 'a^b$c$d$e$f$g' },
    { input: 'a^b$c$d$e$f$g$', expected: 'a^b$c$d$e$f$g' },
    { input: '^a^b$c$d$e$f$g', expected: 'a^b$c$d$e$f$g' },
    { input: 'a^b$c$d$e$f$g', expected: 'a^b$c$d$e$f$g' },
    { input: '^a^b$c$d$e$f$g$h$', expected: 'a^b$c$d$e$f$g$h' },
    { input: 'a^b$c$d$e$f$g$h$', expected: 'a^b$c$d$e$f$g$h' },
    { input: '^a^b$c$d$e$f$g$h', expected: 'a^b$c$d$e$f$g$h' },
    { input: 'a^b$c$d$e$f$g$h', expected: 'a^b$c$d$e$f$g$h' },
    { input: '^a^b$c$d$e$f$g$h$i$', expected: 'a^b$c$d$e$f$g$h$i' },
    { input: 'a^b$c$d$e$f$g$h$i$', expected: 'a^b$c$d$e$f$g$h$i' },
    { input: '^a^b$c$d$e$f$g$h$i', expected: 'a^b$c$d$e$f$g$h$i' },
    { input: 'a^b$c$d$e$f$g$h$i', expected: 'a^b$c$d$e$f$g$h$i' },
    { input: '^a^b$c$d$e$f$g$h$i$j$', expected: 'a^b$c$d$e$f$g$h$i$j' },
    { input: 'a^b$c$d$e$f$g$h$i$j$', expected: 'a^b$c$d$e$f$g$h$i$j' },
    { input: '^a^b$c$d$e$f$g$h$i$j', expected: 'a^b$c$d$e$f$g$h$i$j' },
    { input: 'a^b$c$d$e$f$g$h$i$j', expected: 'a^b$c$d$e$f$g$h$i$j' },
    { input: '^a^b$c$d$e$f$g$h$i$j$k$', expected: 'a^b$c$d$e$f$g$h$i$j$k' },
    { input: 'a^b$c$d$e$f$g$h$i$j$k$', expected: 'a^b$c$d$e$f$g$h$i$j$k' },
    { input: '^a^b$c$d$e$f$g$h$i$j$k', expected: 'a^b$c$d$e$f$g$h$i$j$k' },
    { input: 'a^b$c$d$e$f$g$h$i$j$k', expected: 'a^b$c$d$e$f$g$h$i$j$k' },
  ]

  testCases.forEach(({ input, expected }) => {
      const result = removeAnchors(input)
  })
ready

Revisions

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