multiple string vs regex replace

Benchmark created on


Description

Performance test for multiple string replace operations compared with using a single regex replace

Setup

const str = "-a.b.c.d,e,f,1~2~3~4-5-6--"
const loops = 100000

Test runner

Ready to run.

Testing in
TestOps/sec
regex
const results = []
for (let i = 0; i < loops; i++) {
	results.push(str.replaceAll(/[-.,~-]/g, ''))
}
ready
1 string replacements
const results = []
for (let i = 0; i < loops; i++) {
	results.push(str.replaceAll('-', ''))
}
ready
2 string replacements
const results = []
for (let i = 0; i < loops; i++) {
	results.push(str.replaceAll('-', '').replaceAll('-', ''))
}
ready
3 string replacements
const results = []
for (let i = 0; i < loops; i++) {
	results.push(str.replaceAll('-', '').replaceAll('.', '').replaceAll('-', ''))
}
ready
4 string replacements
const results = []
for (let i = 0; i < loops; i++) {
	results.push(str.replaceAll('-', '').replaceAll('.', '').replaceAll(',', '').replaceAll('-', ''))
}
ready
5 string replacements
const results = []
for (let i = 0; i < loops; i++) {
	results.push(str.replaceAll('-', '').replaceAll('.', '').replaceAll(',', '').replaceAll('~', '').replaceAll('-', ''))
}
ready

Revisions

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