filter vs flatmap

Benchmark created on


Setup

function mymap(xs, f) {
	const r = [];
	for (let i = 0; i < xs.length; i++) {
		r.push(f(xs[i]));
	}
	return r;
}

function myflatmap(xs, f) {
	const r = [];
	for (let i = 0; i < xs.length; i++) {
		r.push(... f(xs[i]));
	}
	return r;
}

function maponly(s) {
	return s.split(/[^a-z0-9]+/i).map(_ => _[0].toUpperCase() + _.slice(1)).join('');
}

function filterish(s) {
	return s.split(/[^a-z0-9]+/i).filter(Boolean).map(_ => _[0].toUpperCase() + _.slice(1)).join('');
}

function flatmapish(s) {
	return s.split(/[^a-z0-9]+/i).flatMap(_ => _ ? [_[0].toUpperCase() + _.slice(1)] : []).join('');
}

function quasifiltermapish(s) {
	return s.split(/[^a-z0-9]+/i).flatMap(_ => _ ? _[0].toUpperCase() + _.slice(1) : []).join('');
}

function skel_filterish(s) {
	return s.split(/[^a-z0-9]+/i).filter(Boolean).map(_ => 'a').join('');
}

function skel_maponly(s) {
	return s.split(/[^a-z0-9]+/i).map(_ => 'a').join('');
}

function skel_flatmapish(s) {
	return s.split(/[^a-z0-9]+/i).flatMap(_ => 'a').join('');
}


function my_filterish(s) {
	return mymap(s.split(/[^a-z0-9]+/i).filter(Boolean), _ => _[0].toUpperCase() + _.slice(1)).join('');
}

function my_flatmapish(s) {
	return myflatmap(s.split(/[^a-z0-9]+/i), _ => _ ? [_[0].toUpperCase() + _.slice(1)] : []).join('');
}

function direct(s) {
	const pieces = s.split(/[^a-z0-9]+/i);
	const r = [];
	for (let i = 0; i < pieces.length; i++) {
		const _ = pieces[i];
		if (_) {
			r.push(_[0].toUpperCase() + _.slice(1));
		}
	}
	return r.join('');
}

function direct_flatmap(s) {
	const pieces = s.split(/[^a-z0-9]+/i);
	const r = [];
	for (let i = 0; i < pieces.length; i++) {
		const _ = pieces[i];
		if (_) {
			const rs = [_[0].toUpperCase() + _.slice(1)];
			r.push(... rs);
		}
	}
	return r.join('');
}

function direct_flatmap2(s) {
	const pieces = s.split(/[^a-z0-9]+/i);
	const r = [];
	for (let i = 0; i < pieces.length; i++) {
		const _ = pieces[i];
		const rs = _ ? [_[0].toUpperCase() + _.slice(1)] : [];
		r.push(... rs);
	}
	return r.join('');
}

function specialcase(s) {
	return s && s.split(/[^a-z0-9]+/i).map(_ => _[0].toUpperCase() + _.slice(1)).join('');
}

Test runner

Ready to run.

Testing in
TestOps/sec
flatmap
flatmapish('this-is-a-longish-input');
ready
filterish
filterish('this-is-a-longish-input');
ready
quasifiltermapish
quasifiltermapish('this-is-a-longish-input');
ready
skel_filterish
skel_filterish('this-is-a-longish-input');
ready
skel_flatmapish
skel_flatmapish('this-is-a-longish-input');
ready
maponly
maponly('this-is-a-longish-input');
ready
skel_maponly
skel_maponly('this-is-a-longish-input');
ready
my_filterish
my_filterish('this-is-a-longish-input');
ready
my_flatmapish
my_flatmapish('this-is-a-longish-input');
ready
direct
direct('this-is-a-longish-input');
ready
direct_flatmap
direct_flatmap('this-is-a-longish-input');
ready
direct_flatmap2
direct_flatmap2('this-is-a-longish-input');
ready
specialcase
specialcase('this-is-a-longish-input');
ready

Revisions

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