escaping characters

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
string only, legacy for loop, mapping object, no functions
const source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789*+'#-_.:,;<>!§$%&/()=?´`\"^°\\";
let result = "";

const map = {
	"|": "\\|",
	"\"": "\\\"",
	"$": "\\$",
}

for ( let i = 0, n = source.length; i < n; i++ ) {
	const ch = source[i];
	result += map[ch] ?? ch;
}
ready
replace with callback
const source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789*+'#-_.:,;<>!§$%&/()=?´`\"^°\\";

const map = {
	"|": "\\|",
	"\"": "\\\"",
	"$": "\\$",
}

const result = source.replace( /[|"$]/g, c => map[c] );
ready

Revisions

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