Segment String

Benchmark created on


Description

Which is the fastest way to add a segmentation character to a string.

Setup

const code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234"

Test runner

Ready to run.

Testing in
TestOps/sec
Loop
  const parts = [];
  for (let i = 0; i < code.length; i += 5) {
    parts.push(code.slice(i, i + 5));
  }
  const result = parts.join(" ");
ready
Regex Match
const regex = /(.{5})/gm;
const result = code.match(regex).join(" ");
ready
Regex Replace
const regex = /(.{5})/gm;
const subst = `$& `;
const result = code.replace(regex, subst).trim();
ready

Revisions

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