If check vs set check

Benchmark created on


Setup

var items = [1,2,3,4,5,6,7];
window.cachedSet = new Set([1,2,3,4]);

function getRandomItem() {
	return items[Math.floor(Math.random()*items.length)];	
}

Test runner

Ready to run.

Testing in
TestOps/sec
Static search - Creating the set each time
(function() {
    var searchTarget = 4;
	var set = new Set([1,2,3,4]);
	var condition = set.has(searchTarget);
})();
ready
Static search - Reusing the set above
(function() {
	var searchTarget = 4;
	var set = window.cachedSet;
	var condition = set.has(searchTarget);
})();
ready
Static search - if checks
(function() {
	var searchTarget = 4;
	var condition = searchTarget === 1 || searchTarget === 2 || searchTarget === 3;
})();
ready
Dynamic search - Creating the set each time
(function() {
	var searchTarget = getRandomItem();
    var set = new Set([1,2,3,4]);
    var condition = set.has(searchTarget);
 })();
ready
Dynamic search - Reusing the set above
(function() {
	var searchTarget = getRandomItem();
    var set = window.cachedSet;
    var condition = set.has(searchTarget);
 })();
ready
Dynamic search - If checks
(function() {
	var searchTarget = getRandomItem();
	var condition = searchTarget === 1 || searchTarget === 2 || searchTarget === 3;
})();
ready

Revisions

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