Was curious about this since Object.prototype
is an ancestor of any array instance, therefore it must be bulkier than an object and slower when it comes to property lookup.
In my test I store a list of strings as array elements and an object keys. Then I measure how much time it takes to check for existance of each key in both object and array implementations. Repeat 100000 times.
Generally, objects happen to be faster. Null objects are significantly faster on chromium.
{
const iterations = 1000000;
let arrTotal = 0;
let objTotal = 0;
let nullObjTotal = 0;
let a = '';
let keys = [
"The standard Lorem Ipsum passage",
"used since the 1500sLorem ipsum dolor sit amet",
"consectetur adipiscing elit",
"Ut enim ad minim veniam",
"Excepteur sint occaecat cupidatat non proident",
"sunt in culpa qui officia deserunt mollit anim id est laborum",
"Section 1",
"32 of de Finibus Bonorum et Malorum",
"totam rem aperiam",
"Neque porro quisquam est",
"qui dolorem ipsum quia dolor sit amet",
"consectetur",
"adipisci velit",
"Ut enim ad minima veniam",
"the master-builder of human happiness",
"No one rejects",
"dislikes",
"or avoids pleasure itself",
"because it is pleasure",
"because it is pain",
"To take a trivial example",
"which of us ever undertakes laborious physical exercise",
"33 of de Finibus Bonorum et Malorum",
"similique sunt in culpa qui officia deserunt mollitia animi",
"id est laborum et dolorum fuga",
"Et harum quidem rerum facilis est et expedita distinctio",
"Nam libero tempore",
"omnis voluptas assumenda est",
"omnis dolor repellendus",
"Itaque earum rerum hic tenetur a sapiente delectus",
"1914 translation by H",
"RackhamOn the other hand",
"so blinded by desire",
"These cases are perfectly simple and easy to distinguish",
"In a free hour",
"every pleasure is to be welcomed and every pain avoided",
"or else he endures pains to avoid worse pains"
];
let nullObj = Object.create(null);
for (let key of keys) nullObj[key] = null;
let obj = {
"The standard Lorem Ipsum passage": null,
"used since the 1500sLorem ipsum dolor sit amet": null,
"consectetur adipiscing elit": null,
"Ut enim ad minim veniam": null,
"Excepteur sint occaecat cupidatat non proident": null,
"sunt in culpa qui officia deserunt mollit anim id est laborum": null,
"Section 1": null,
"32 of de Finibus Bonorum et Malorum": null,
"totam rem aperiam": null,
"Neque porro quisquam est": null,
"qui dolorem ipsum quia dolor sit amet": null,
"consectetur": null,
"adipisci velit": null,
"Ut enim ad minima veniam": null,
"the master-builder of human happiness": null,
"No one rejects": null,
"dislikes": null,
"or avoids pleasure itself": null,
"because it is pleasure": null,
"because it is pain": null,
"To take a trivial example": null,
"which of us ever undertakes laborious physical exercise": null,
"33 of de Finibus Bonorum et Malorum": null,
"similique sunt in culpa qui officia deserunt mollitia animi": null,
"id est laborum et dolorum fuga": null,
"Et harum quidem rerum facilis est et expedita distinctio": null,
"Nam libero tempore": null,
"omnis voluptas assumenda est": null,
"omnis dolor repellendus": null,
"Itaque earum rerum hic tenetur a sapiente delectus": null,
"1914 translation by H": null,
"RackhamOn the other hand": null,
"so blinded by desire": null,
"These cases are perfectly simple and easy to distinguish": null,
"In a free hour": null,
"every pleasure is to be welcomed and every pain avoided": null,
"or else he endures pains to avoid worse pains": null
};
let arr = [
"The standard Lorem Ipsum passage",
"used since the 1500sLorem ipsum dolor sit amet",
"consectetur adipiscing elit",
"Ut enim ad minim veniam",
"Excepteur sint occaecat cupidatat non proident",
"sunt in culpa qui officia deserunt mollit anim id est laborum",
"Section 1",
"32 of de Finibus Bonorum et Malorum",
"totam rem aperiam",
"Neque porro quisquam est",
"qui dolorem ipsum quia dolor sit amet",
"consectetur",
"adipisci velit",
"Ut enim ad minima veniam",
"the master-builder of human happiness",
"No one rejects",
"dislikes",
"or avoids pleasure itself",
"because it is pleasure",
"because it is pain",
"To take a trivial example",
"which of us ever undertakes laborious physical exercise",
"33 of de Finibus Bonorum et Malorum",
"similique sunt in culpa qui officia deserunt mollitia animi",
"id est laborum et dolorum fuga",
"Et harum quidem rerum facilis est et expedita distinctio",
"Nam libero tempore",
"omnis voluptas assumenda est",
"omnis dolor repellendus",
"Itaque earum rerum hic tenetur a sapiente delectus",
"1914 translation by H",
"RackhamOn the other hand",
"so blinded by desire",
"These cases are perfectly simple and easy to distinguish",
"In a free hour",
"every pleasure is to be welcomed and every pain avoided",
"or else he endures pains to avoid worse pains"
];
for (let i = 0, stamp = 0, length = keys.length; i < iterations; ++i) {
stamp = performance.now();
for (let j = 0; j < length; ++j) {
if (keys[j] in obj) a = keys[j];
}
objTotal += (performance.now() - stamp)/1000;
stamp = performance.now();
for (let j = 0; j < length; ++j) {
if (~arr.indexOf(keys[j])) a = keys[j];
}
arrTotal += (performance.now() - stamp)/1000;
stamp = performance.now();
for (let j = 0; j < length; ++j) {
if (keys[j] in nullObj) a = keys[j];
}
nullObjTotal += (performance.now() - stamp)/1000;
}
console.log(`Array total: ${arrTotal}; Array avarage: ${arrTotal/iterations}(s).`);
console.log(`Object total: ${objTotal}; Object avarage: ${objTotal/iterations}(s).`);
console.log(`Null object total: ${nullObjTotal}; Null object avarage: ${nullObjTotal/iterations}(s).`);
}