containsAny (v2)

Revision 2 of this benchmark created on


Setup

A = new Array(1000).fill(undefined).map(() => {
	return Math.floor(1000 * Math.random());
});
B = null;


function containsAnyLoop(i,kal) {
	for(var j=0;j<i.length;j++) {
		for(var el in kal) {
			if(i[j]===kal[el]) {
				return true;
			}
		}
	}
	return false;
}
function containsAnyLoop2(i,kal) {
	for(var j=0;j<i.length;j++) {
		for(var k=0;k<kal.length;k++) {
			if(i[j]===kal[k]) {
				return true;
			}
		}
	}
	return false;
}

function containsAnyLoopInverse(i,kal) {
	for(var el in kal) {
		let ELEM = kal[el];
		for(var j=0;j<i.length;j++) {
			if(i[j]=== ELEM) {
				return true;
			}
		}
	}
	return false;
}

function containsAnyFunctional(i,kal) {
	return kal.some((elem) => i.includes(elem));
}

Test runner

Ready to run.

Testing in
TestOps/sec
Sanity
for(let i = 0; i < 1000; i++) {
	let RL = containsAnyLoop([i, i * 2], A);
	let RL2 = containsAnyLoop2([i, i * 2], A);
	let RLI = containsAnyLoopInverse([i, i * 2], A);
	let F = containsAnyFunctional([i, i * 2], A);
	if((F != RL) || (RLI != RL) || (RL2 != RL)) {
		console.log([i, i * 2], F, RL, RL2, RLI, JSON.stringify(A));
		throw new Error([i, A]);
	}
}
ready
Functional
let result = 0;
for(let i = 0; i < 1000; i++) {
	result += containsAnyFunctional([i, i * 2], A);
}
console.log(result);
ready
Loop
let result = 0;
for(let i = 0; i < 1000; i++) {
	result += containsAnyLoop([i, i * 2], A);
}
console.log(result);
ready
Loop Inverse
let result = 0;
for(let i = 0; i < 1000; i++) {
	result += containsAnyLoopInverse([i, i * 2], A);
}
console.log(result);
ready
Loop2
let result = 0;
for(let i = 0; i < 1000; i++) {
	result += containsAnyLoop2([i, i * 2], A);
}
console.log(result);
ready

Revisions

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