在Javascript中迭代对象属性的最快方法是什么? [英] What's the fastest way to iterate over an object's properties in Javascript?

查看:113
本文介绍了在Javascript中迭代对象属性的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以迭代一个对象的属性,如下所示:

I know that I can iterate over an object's properties like this:

for (property in object)
{
    // do stuff
}

我也知道最快的方式在Javascript中迭代数组是使用递减while循环:

I also know that the fastest way to iterate over an array in Javascript is to use a decreasing while loop:

var i = myArray.length;
while (i--)
{
    // do stuff fast
}

我想知道是否有类似于减少while循环的内容来迭代对象的属性。

I'm wondering if there is something similar to a decreasing while loop for iterating over an object's properties.

编辑:只关注与可枚举性有关的答案 - 我不是。

just a word about the answers concerned with enumerability - I'm not.

推荐答案

1)有许多不同的枚举属性的方法:

1) There are many different ways to enumerate properties:


  • for..in (迭代对象及其原型链的可枚举属性)

  • Object.keys(obj) 返回可直接在对象上找到的可枚举属性的数组(不在其原型链中)

  • Object.getOwnPropertyNames(obj) 返回直接在对象上找到的所有属性(可枚举或不可枚举)的数组。

  • 如果你正在处理具有相同形状的多个对象(支柱组) erties),预编译迭代代码可能有意义(参见此处的其他答案)。

  • for..of 不能用于迭代任意对象,但可以与 Map Set ,它们都是某些用例的普通对象的合适替代品。

  • ...

  • for..in (iterates over enumerable properties of the object and its prototype chain)
  • Object.keys(obj) returns the array of the enumerable properties, found directly on the object (not in its prototype chain)
  • Object.getOwnPropertyNames(obj) returns an array of all properties (enumerable or not) found directly on the object.
  • If you're dealing with multiple objects of the same "shape" (set of properties), it might make sense to "pre-compile" the iteration code (see the other answer here).
  • for..of can't be used to iterate an arbitrary object, but can be used with a Map or a Set, which are both suitable replacements for ordinary Objects for certain use-cases.
  • ...

也许如果你说出原来的问题,有人可以提出一种优化方法。

Perhaps if you stated your original problem, someone could suggest a way to optimize.

2)我发现很难相信实际的枚举比你对循环体中的属性做的更多。

2) I find it hard to believe that the actual enumeration is taking more than whatever you do with the properties in the loop body.

3)你没有指定你正在开发什么平台。答案可能取决于它,可用的语言功能也取决于它。例如。在大约2009年的SpiderMonkey(Firefox JS解释器)中,您可以为每个使用(在arr中使用var x) docs )如果您确实需要值,而不是键。对于(var i in arr){var x = arr [i];它比快。 ...}

3) You didn't specify what platform you're developing for. The answer would probably depend on it, and the available language features depend on it too. E.g. in SpiderMonkey (Firefox JS interpreter) circa 2009 you could use for each(var x in arr) (docs) if you actually needed the values, not the keys. It was faster than for (var i in arr) { var x = arr[i]; ... }.

V8在某个时刻回退 for..in 的性能,然后修复它。以下是2017年V8中 for..in 内部的帖子: https://v8project.blogspot.com/2017/03/fast-for-in-in-v8.html

V8 at some point regressed the performance of for..in and subsequently fixed it. Here's a post on the internals of for..in in V8 in 2017: https://v8project.blogspot.com/2017/03/fast-for-in-in-v8.html

4)您可能只是没有将它包含在您的代码段中,但更快的方法是执行 for..in 迭代确保在循环中使用的变量在包含循环的函数内声明,即:

4) You probably just didn't include it in your snippet, but a faster way to do a for..in iteration is to make sure the variables you use in the loop are declared inside the function containing the loop, i.e.:

//slower
for (property in object) { /* do stuff */ }

//faster
for (var property in object) { /* do stuff */ }

5)与(4)相关:在尝试优化Firefox扩展时,我曾经注意到将一个紧密的循环提取到一个单独的功能改善了它的性能(链接)。 (显然,这并不意味着你应该总是这样做!)

5) Related to (4): while trying to optimize a Firefox extension I once noticed that extracting a tight loop into a separate function improved its performance (link). (Obviously, it doesn't mean you should always do that!)

这篇关于在Javascript中迭代对象属性的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆