最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

performance - Efficiently counting the number of keysproperties of an Object in JavaScript - Stack Overflow

programmeradmin0浏览0评论

This question is almost identical to How to efficiently count the number of keys/properties of an object in JavaScript?.

I want to know one extra piece of information: what is a "constant-time" way of determining the number of keys in an Object? I am mostly concerned with doing this in Node.JS, as most Objects on the browser aren't too large to be of great concern.

EDIT: It appears that Object.keys(obj).length returns in linear time O(n) in Google Chrome and in Node.JS (i.e. dependent on the number of keys in obj). Is there a better O(1) method?

I did some testing in Node.JS (source is below)

var tests = [10e3, 10e4, 10e5, 10e6]
for(j in tests) {
    var obj = {};
    for(i = 0; i < tests[j]; i++)
        obj[i] = i;
    console.time('test' + tests[j]);
    Object.keys(obj).length;
    console.timeEnd('test' + tests[j]);
}

For n = 10e3, 10e4, 10e5, 10e6... results are:

test10000: 5ms
test100000: 20ms
test1000000: 371ms
test10000000: 4009ms

This question is almost identical to How to efficiently count the number of keys/properties of an object in JavaScript?.

I want to know one extra piece of information: what is a "constant-time" way of determining the number of keys in an Object? I am mostly concerned with doing this in Node.JS, as most Objects on the browser aren't too large to be of great concern.

EDIT: It appears that Object.keys(obj).length returns in linear time O(n) in Google Chrome and in Node.JS (i.e. dependent on the number of keys in obj). Is there a better O(1) method?

I did some testing in Node.JS (source is below)

var tests = [10e3, 10e4, 10e5, 10e6]
for(j in tests) {
    var obj = {};
    for(i = 0; i < tests[j]; i++)
        obj[i] = i;
    console.time('test' + tests[j]);
    Object.keys(obj).length;
    console.timeEnd('test' + tests[j]);
}

For n = 10e3, 10e4, 10e5, 10e6... results are:

test10000: 5ms
test100000: 20ms
test1000000: 371ms
test10000000: 4009ms
Share Improve this question edited May 23, 2017 at 12:25 CommunityBot 11 silver badge asked Oct 31, 2011 at 16:25 BMinerBMiner 17.1k12 gold badges54 silver badges53 bronze badges 11
  • Nope. I'm feeling lazy today... :/ Case of the Mondays, I suppose. – BMiner Commented Oct 31, 2011 at 16:36
  • 2 I suspect that getting the ".length" from the result of calling "Object.keys()" is constant-time, but I also suspect that calling "Object.keys()" is linear in the number of properties. – Pointy Commented Oct 31, 2011 at 16:36
  • 1 jsperf.com/object-keys-performance 100 times as many items in the object causes it to be 200 times as slow on Chrome. – pimvdb Commented Oct 31, 2011 at 16:40
  • Thanks, @pimvdb -- that's really sad news. :( Is there any constant-time key-counting method in Node.JS? – BMiner Commented Oct 31, 2011 at 16:43
  • 1 @BMiner sorry I meant Yes Object.keys(o).length is O(n). .length on an array is O(1). – Raynos Commented Nov 1, 2011 at 14:25
 |  Show 6 more comments

3 Answers 3

Reset to default 7

After a bit of research, there is no way to determine the number of keys in a JavaScript Object in constant time, at least not in Node... and not quite yet. Node internally keeps track of this information, but it does not expose it, since there is no method to do so in ECMA-262 5th.

It's worth noting that Harmony (ECMA version 6) may natively support Maps and Sets. Not sure what the spec for these will turn out to be.

I am told that we need to bring this up with TC39 committee.

Bug report for V8: http://code.google.com/p/v8/issues/detail?id=1800

ECMA 6 harmony introduces Map and Set classes which you can probably utilize (in the future :)

var map = new Map;
map.set('a', 'b');
console.log(map.size); // prints 1

I believe it should have complexity O(1), not tried though. You can run it in node 0.11+ via node --harmony script.js.


Another way is to use Proxy class which also has been added in harmony.

See the source, specifically GetLocalElementKeys

v8 objects.cc

发布评论

评论列表(0)

  1. 暂无评论