containsAny (v6)

Revision 6 of this benchmark created on


Setup

A = new Array(1000).fill(undefined).map((_, i) => {
	return (i * 2048) % 1000;
});


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 containsAnyLoop3(i,kal) {
	for(let j=0;j<i.length;j++) {
		for(let k=0;k<kal.length;k++) {
			if(i[j]===kal[k]) {
				return true;
			}
		}
	}
	return false;
}

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


function containsAnyMix(i,kal) {
	for(var j=0;j<i.length;j++) {
		if(kal.includes(i[j])) {
			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));
}

Teardown

A = null;

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 RLI2 = containsAnyLoopInverse2([i, i * 2], A);
	let F = containsAnyFunctional([i, i * 2], A);
	let M = containsAnyMix([i, i * 2], A)
	if((F != RL) || (RLI != RL) || (RL2 != RL) || (RLI2 != RL) || (M != RL)) {
		console.log([i, i * 2], F, RL, RL2, RLI, RLI2, JSON.stringify(A));
		throw new Error([i, A]);
	}
}
ready
Functional
let result = 0;
let CUR = [0, 0];
for(let i = 0; i < 1000; i++) {
	CUR[0] = i;
	CUR[1] = i * 2;
	result += containsAnyFunctional(CUR, A);
}
console.log(result);
ready
Loop
let result = 0;
let CUR = [0, 0];
for(let i = 0; i < 1000; i++) {
	CUR[0] = i;
	CUR[1] = i * 2;
	result += containsAnyLoop(CUR, A);
}
console.log(result);
ready
Loop Inverse
let result = 0;
let CUR = [0, 0];
for(let i = 0; i < 1000; i++) {
	CUR[0] = i;
	CUR[1] = i * 2;
	result += containsAnyLoopInverse([i, i * 2], A);
}
console.log(result);
ready
Loop2
let result = 0;
let CUR = [0, 0];
for(let i = 0; i < 1000; i++) {
	CUR[0] = i;
	CUR[1] = i * 2;
	result += containsAnyLoop2(CUR, A);
}
console.log(result);
ready
Loop2 Inverse
let result = 0;
let CUR = [0, 0];
for(let i = 0; i < 1000; i++) {
	CUR[0] = i;
	CUR[1] = i * 2;
	result += containsAnyLoopInverse2(CUR, A);
}
console.log(result);
ready
Mix
let result = 0;
let CUR = [0, 0];
for(let i = 0; i < 1000; i++) {
	CUR[0] = i;
	CUR[1] = i * 2;
	result += containsAnyMix(CUR, A);
}
console.log(result);
ready
Loop3
let result = 0;
let CUR = [0, 0];
for(let i = 0; i < 1000; i++) {
	CUR[0] = i;
	CUR[1] = i * 2;
	result += containsAnyLoop3(CUR, A);
}
console.log(result);
ready

Revisions

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