示例1:
["member1", "member2",...,..., "member100000"]
示例2:
{
"member1": true, // (doesn't really need values, only keys :/)
"member2": true,
"...",
"member100000": true
}
我将成员存储在每个内容的数组中,如例1所示,但这样做我必须遍历我的数组中的49999项,找到成员50000,所以我想只是检查javascript对象中是否定义了特定的键是一种更好的方法,虽然我不需要存储值,但只检查键是否未定义?
我需要的是能够检查是否例如。 " member50000"作为我的数组中的值存在 - 或作为我对象中的键。
我做了一些基准测试,但我不确定我是否得出了正确的结论,或者我在比较中做错了什么:http://jsperf.com/lolda123
根据上面的测试结果,那么可以公平地断定在值为布尔值(true)的对象中保存键/值对,并且执行if(obj["member50000"])
是表现最佳的选项吗?即使没有给定密钥的财产,即使存在?正如我所看到的,根据我的测试结果,检查密钥本身的存在,在性能方面似乎要贵得多,但检查密钥是否存在,实际上是我所需要的。
我不在乎价值,所以我在这里遗漏了一些东西,或者为什么更好的解决方案看起来像是通过钥匙查找价值的那个,而不仅仅是查找钥匙,在对象里面?
答案 0 :(得分:2)
我认为同样的事情,使用对象会更快,我证明自己错了!
我使用与NodeJS 6.3(V8引擎)上的jsPerf相同的堆栈运行性能测试:
https://github.com/amoldavsky/js-array-indexof-vs-hashmap
结果是:
如果您认为我在任何测试中犯了错误,请随意提出,我们可以重新测试。虽然到目前为止看起来像array.indexOf要快得多。
答案 1 :(得分:1)
obj.hasOwnProperty
比arr.indexOf
http://jsperf.com/array-hasownproperty-vs-array-indexof
使用arr.indexOf
也应该比你做的任何循环都快。
答案 2 :(得分:0)
对此颇为好奇,因为Object.prototype
是任何数组实例的祖先,因此它必须比对象大并且在属性查找时要慢。
在测试中,我将字符串列表存储为数组元素和对象键。然后,我测量检查对象和数组实现中每个键是否存在所需的时间。重复100000次。
通常,对象碰巧会更快。空物体在铬上的速度要快得多。
{
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).`);
}