If check vs set check (v2)

Revision 2 of this benchmark created on


Setup

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

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(window.setContents);
	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(window.setContents);
    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.