string handling

Benchmark created on


Setup

let template = "${value1}, ${value2}, ${value3}";
let data = {
	value1: 1,
	value2: "2",
	value3: "$3",
};

Test runner

Ready to run.

Testing in
TestOps/sec
replace
let srcValue = template;
for (const [paramName, paramValue] of Object.entries(data)) {
	let value = paramValue;
	if (typeof paramValue === 'string') {
		value = paramValue.replace(new RegExp('\\$', 'g'), '$$$$');
	}
	const placeholderWords = new RegExp(`\\\${.*?${paramName}.*?}`, 'g');
	srcValue = srcValue.replace(placeholderWords, '' + value);
}
ready
split and join
let srcValue = template;
for (const [paramName, paramValue] of Object.entries(data)) {
	srcValue = srcValue.split(`\${${paramName}}`).join(paramValue);
}
ready

Revisions

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