Escape newlines

Benchmark created on


Setup

const strs = [
  'hello\nworld',
  'hello\t\tworld',
  'hello\r\r\rworld',
  '\t\r\n\t\r\n',
  '\\t\\r\\n\\t\\r\\n',
]

Test runner

Ready to run.

Testing in
TestOps/sec
JSON.stringify()
for (const str of strs) {
  console.log(JSON.stringify(str).slice(1, -1))
}

ready
replaceAll()
for (const str of strs) {
  console.log(
    str
      .replaceAll('\n', '\\n')
      .replaceAll('\r', '\\r')
      .replaceAll('\t', '\\t')
  )
}

ready

Revisions

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