Array check vs bitwise flags

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
With array methods
const cookies = {
	a: ['performance'],
	b: ['performance', 'analytics'],
	c: ['tracking', 'analytics']
}

const flags = ['performance', 'analytics']
const check = (cookie) => cookie.every(category => flags.includes(category))

let result
for (let i = 0; i < 10000; i++) {
	result = check(cookies.a)
	result = check(cookies.b)
	result = check(cookies.c)
}
ready
With bitwise operations
const categories = {
	PERFORMANCE: 1,
	ANALYTICS: 2,
	TRACKING: 4
}

const cookies = {
	a: categories.PERFORMANCE + categories.ANALYTICS,
	b: categories.ANALYTICS,
	c: categories.TRACKING + categories.ANALYTICS
}

const flags = categories.PERFORMANCE + categories.ANALYTICS

const check = (cookie) => (flags & cookie) === cookie


let result
for (let i = 0; i < 10000; i++) {
	result = check(cookies.a)
	result = check(cookies.b)
	result = check(cookies.c)
}
ready

Revisions

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