- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在做一个项目,我需要从一个巨大的用户数据列表中提取一个被排除的用户列表。这让我想知道在 array
中使用带有排除 id 的双重 for 循环
是否更快。或者,如果将 id 放入对象属性并使用 .hasOwnProperty()
会更快。
var mainList = LARGE JSON OBJECT OF DATA.
var eArray = ["123456","234567","345678","456789","012345"];
var eObject = {"123456":"0","234567":"0","345678":"0","456789":"0","012345":"0"};
使用双重 For 循环
方法:
for(i=0; i < mainList.length; i++){
for(j=0; j < eArray.length; j++){
if(mainList[i]['id'] === eArray[j]){
//Do Something
}
}
}
使用 .hasOwnProperty()
方法:
for(i=0; i < mainList.length; i++){
if(eObject.hasOwnProperty(mainList[i]['id'])){
//Do Something
}
}
我意识到还有其他方法可以使循环更快,例如将长度存储在变量中。我已尝试对此进行简化。
感谢您提供任何信息。
最佳答案
您错过了第三种更快的选择。如果您没有以任何方式修改 Object.prototype
,并且 ID 不太可能是原型(prototype)值(如 valueOf
等),您可以简单地像这样使用 for
循环:
for(var i=0; i < mainList.length; i++)
{
if (eObject[mainList[i].id] !== undefined)
{//or typeof eObject[mainList[i].id] !== 'undefined'
//do something
}
}
检查 the updated JSPref ,这是目前最快的方法(57,252,850 ops/sec 对比双循环 17,503,538 ops/sec)
关于javascript - For Loop 和 .hasOwnProperty 哪个更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17648395/
对版主的澄清由于一些版主在扫描问题时速度有点快,我必须强调我不是在问为什么要使用 Object.prototype.hasOwnProperty.call 而不是 myObject.hasOwnPro
代码一: obj.hasOwnProperty(prop); 代码二: const hasOwnProperty = Object.prototype; hasOwnProperty.call(obj
这个问题在这里已经有了答案: Why use Object.prototype.hasOwnProperty.call(myObj, prop) instead of myObj.hasOwnPro
也许这是一个新手问题,但我找不到或想不出解释。 启动 Node.js 控制台,然后: > global.hasOwnProperty === hasOwnProperty true 那为什么 > gl
在eslint规则中guard-for-in ,直接使用for in是不正确的。好的做法是 for (key in foo) { if (Object.prototype.hasOwnProp
如果我理解正确的话,JavaScript 中的每个对象都继承自 Object 原型(prototype),这意味着 JavaScript 中的每个对象都可以通过其原型(prototype)链访问 ha
我正在尝试让我的某些类型具有某种多重“继承”,如下所示: UIControls.ClickableMesh.prototype = Object.create(THREE.Mesh.prototype
我试图发现一个对象是否具有某些属性并且我在使用 hasOwnProperty 方法时遇到了问题。 我在数组上使用该方法(我知道文档说明了一个字符串)。 以下行返回 true: { "a": 1, "b
谁能解释一下空对象上调用的 hasOwnProperty 的作用?为什么要使用它? __hasProp = {}.hasOwnProperty 我在开始使用 coffescript 进行开发时发现了这
我想使用ES6代理来捕获以下常见代码: for (let key in trapped) { if (!Object.prototype.hasOwnProperty.call(obj, ke
使用 angularjs 和 firebase 编写一个函数,该函数应该检查是否可以在数据库中找到 currentUser。目前“attendings”属性仅包含一个用户,即“Peter Pan” 服
我有一个非常简单的问题。访问对象的属性(例如object[property])和属性的数量是否有任何性能关系?是否存在一些内部循环或其他问题,关于 hasOwnProperty - 任何循环或只是像
我试图理解为什么当我调用下面的函数时结果是“否”,因为属性 c 应该存在。有谁知道为什么?谢谢!!! var letters = function() { this.a = 5; th
在 Javascript 中查找散列中的所有值我看到了以下代码: var myHash = {}; myHash['one'] = 1; myHash['two'] = 2; for (var key
我有以下数据: trace = { "name":"foo", "dataref": { "xdata":"n", "ydata":"m" } }; 我想检查对象是否具有
通过某些值,hasOwnProperty调用将引发错误。 让我们检查以下代码 null.hasOwnProperty('bar') //error undefined.hasOwnProperty('
为什么是真的?对象中的方法foo怎么写? Object.prototype.foo = function(obj) { for(var i in obj) this[i] = obj[i];
这个问题在这里已经有了答案: JavaScript object detection: dot syntax versus 'in' keyword (5 个答案) 关闭 7 年前。 假设有一个对象
问题陈述: 如果对象具有与 JavaScript 预定义的 方法 相同的 property 名称。它无法执行并给出以下错误。 Uncaught TypeError: obj.hasOwnPropert
为什么此函数返回“No Contact”而不是“Akira”? function lookUpProfile(firstName, prop){ for(i=0;i
我是一名优秀的程序员,十分优秀!