Line Prefixing

Benchmark created on


Setup

const prefix = '[0] ';
const text = Array(100).fill('foo\nbar\nbaz').join('\n') + '\n';
const textForArray = text.slice(0);

Test runner

Ready to run.

Testing in
TestOps/sec
Split + Map + Join
textForArray.split('\n').map((line, i, lines) => {
	return i === lines.length - 1 ? line : prefix + line;
}).join('\n')
ready
Regex Replace with capture group
text.replace(/\n(.)/g, `\n${prefix}$1`)
ready
ReplaceAll
text.replace(/\n$/, '').replaceAll("\n", `\n${prefix}`)
ready

Revisions

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