eql vs includes

Benchmark created on


Setup

const arr = ["GET", "HEAD", "DELETE", "OPTIONS", "TRACE", "CONNECT"];
const set = new Set(arr);
const samples = ["GET", "HEAD", "DELETE", "OPTIONS", "TRACE", "CONNECT", "OTHER"];
const obj = Object.fromEntries(arr.map(q => [q, true]));

function a(method) {
	return method === "GET" || method === "HEAD" || method === "DELETE" || method === "OPTIONS" || method === "TRACE" || method === "CONNECT";
}
function b(method) {
	return arr.includes(method);
}
function c(method) {
	return set.has(method);
}
function d(method) {
	const set = new Set(["GET", "HEAD", "DELETE", "OPTIONS", "TRACE", "CONNECT"]);
	return set.has(method);
}
function e(method) {
	return !!obj[method];
}
function f(method) {
	return !!{GET:true,HEAD:true,DELETE:true,OPTIONS:true,TRACE:true,CONNECT:true}[method];
}
function g(method) {
	return ["GET", "HEAD", "DELETE", "OPTIONS", "TRACE", "CONNECT"].includes(method);
}
function h(method) {
    switch(method) {
		case "GET":return "GET";
		case "HEAD":return "HEAD";
		case "DELETE":return "DELETE";
		case "OPTIONS":return "OPTIONS";
		case "TRACE":return "TRACE";
		case "CONNECT":return "CONNECT";
	}
}

Test runner

Ready to run.

Testing in
TestOps/sec
eql: ?
a(samples[Math.random() * samples.length |0])
ready
includes: ?
b(samples[Math.random() * samples.length |0])
ready
has: ?
c(samples[Math.random() * samples.length |0])
ready
inline set has: ?
d(samples[Math.random() * samples.length |0])
ready
object: ?
e(samples[Math.random() * samples.length |0])
ready
inline object: ?
f(samples[Math.random() * samples.length |0])
ready
inline array: ?
g(samples[Math.random() * samples.length |0])
ready
switch: ?
h(samples[Math.random() * samples.length |0])
ready

Revisions

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