_.intersection vs Set

Benchmark created on


Preparation HTML

<script src="
https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js
"></script>

Setup

const payloads = ["a", "b", "d", "f"];
const entities = new Array(10000).fill(null).map((_, i) => {
	const entity = { id: Math.random(), payloads: [] }
	if (entity.id > 0.5) {
		entity.payloads.push("a", "d");
	} else if (entity.id < 0.2) {
		entity.payloads.push("g", "h");
	}
	return entity;
})

Test runner

Ready to run.

Testing in
TestOps/sec
With lodash intersection
entities.filter(entity => {
	if (!entity.payloads.length) return;
	
	return _.intersection(payloads, entity.payloads).length > 0;
})
ready
With set and some
const set = new Set(payloads);
entities.filter(entity => {
	if (!entity.payloads.length) return;
	
	return entity.payloads.some(p => set.has(p));
})
ready

Revisions

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