Object Lookup vs Comparison Function (v6)

Revision 6 of this benchmark created on


Setup

const CATEGORY_A = "Category A";
const CATEGORY_B = "Category B";
const CATEGORY_C = "Category C";
const CATEGORY_D = "Category D";
const CATEGORY_E = "Category E";

function lookupCategoryName(category) {
	if (category === CATEGORY_A) {
		return "Name A";
	}
	if (category === CATEGORY_B) {
		return "Name B";
	}
	if (category === CATEGORY_C) {
		return "Name C";
	}
	if (category === CATEGORY_D) {
		return "Name D";
	}
	if (category === CATEGORY_E) {
		return "Name E";
	}
}


const LOOKUP_OBJECT = {};
LOOKUP_OBJECT[CATEGORY_A] = "Name A";
LOOKUP_OBJECT[CATEGORY_B] = "Name B";
LOOKUP_OBJECT[CATEGORY_C] = "Name C";
LOOKUP_OBJECT[CATEGORY_D] = "Name D";
LOOKUP_OBJECT[CATEGORY_E] = "Name E";

Test runner

Ready to run.

Testing in
TestOps/sec
Comparison Function
let a = lookupCategoryName("Category A");
let b = lookupCategoryName("Category B");
let c = lookupCategoryName("Category C");
let d = lookupCategoryName("Category D");
let e = lookupCategoryName("Category E");

ready
Object Lookup
let a = LOOKUP_OBJECT["Category A"];
let b = LOOKUP_OBJECT["Category B"];
let c = LOOKUP_OBJECT["Category C"];
let d = LOOKUP_OBJECT["Category D"];
let e = LOOKUP_OBJECT["Category E"];
ready

Revisions

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