Replace indexOf vs Regex

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
IndexOf
const str = 'with-prefix';
if (0 === str.indexOf('with-')) {
	str.replace('with-', 'prefix-');
}
if (0 === str.indexOf('other-')) {
	str.replace('other-', 'prefix-');
}
ready
Regex
const str = 'with-prefix';
str.replace(/^with-/, 'prefix-');
str.replace(/^other-/, 'prefix-');
ready
StartsWith
const str = 'with-prefix';
if (str.startsWith('with-')) {
	str.replace('with-', 'prefix-');
}
if (str.startsWith('other-')) {
	str.replace('other-', 'prefix-');
}
ready

Revisions

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