问题:
What's the fastest way to count the number of keys/properties of an object? 计算对象的键/属性数的最快方法是什么? It it possible to do this without iterating over the object? 是否可以在不迭代对象的情况下执行此操作? ie without doing 即不做
var count = 0;
for (k in myobj) if (myobj.hasOwnProperty(k)) count++;
(Firefox did provide a magic __count__
property, but this was removed somewhere around version 4.) (Firefox确实提供了一个神奇的__count__
属性,但此属性已在版本4的某个位置删除。)
解决方案:
参考一: https://stackoom.com/question/Wns/如何有效地计算JavaScript中对象的键-属性数量参考二: https://oldbug.net/q/Wns/How-to-efficiently-count-the-number-of-keys-properties-of-an-object-in-JavaScript
来源:oschina
链接:https://my.oschina.net/u/4438370/blog/4479622