String startsWith and endsWith vs Regex

Benchmark created on


Description

Test if doing a startsWith and substring, and an endsWith and a slice is more efficient than doing a single regex.

Setup

let result;
const arg = 'name="rad"';

Test runner

Ready to run.

Testing in
TestOps/sec
startsWith and endsWith
if (arg.startsWith('name="')) {
	let name = arg.substring(6);
	if (name.endsWith('"')) {
		name = name.slice(0, -1);
	}
	result = name;
}
ready
Regex
result = (arg.match(/^name="(.+)"$/))?.[1] || '';
ready

Revisions

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