String Methods!

Benchmark created on


Setup

function rand() {
	return Array.from({ length: 20 }, () => 'qwertyuiopasdfghjklzxcvbnm1234567890'[Math.floor(Math.random() * 36)]).join('');
}

const classes = Array.from({ length: 100_000 }, rand);
const output = classes.filter(str => /^[a-z]/.test(str)).join(' ');

Test runner

Ready to run.

Testing in
TestOps/sec
filter join
const filtered = classes.filter(str => /^[a-z]/.test(str)).join(' ');
if (filtered !== output) throw new Error('Not matching');
ready
Reduce
const filtered = classes.reduce((acc, str) => {
	if (/^[a-z]/.test(str)) return acc + ' ' + str;
	return acc;
}, '');
if (filtered.replace(/^ /, '') !== output) throw new Error('Not matching');
ready
forEach append
let acc = '';
classes.forEach(str => {
	if (/^[a-z]/.test(str)) acc += ' ' + str;
});
if (acc.replace(/^ /, '') !== output) throw new Error('Not matching');
ready

Revisions

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