24

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
asked Oct 31, 2011 at 16:25
11
  • Nope. I'm feeling lazy today... :/ Case of the Mondays, I suppose. 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. 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. 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? 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). Commented Nov 1, 2011 at 14:25

3 Answers 3

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

answered Nov 1, 2011 at 13:50
2

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.

answered Jan 16, 2014 at 5:12
-2

See the source, specifically GetLocalElementKeys

v8 objects.cc

answered Oct 31, 2011 at 16:47
2
  • I'm too lazy to do this. English, please. I'd gladly accept your answer. :P Commented Oct 31, 2011 at 16:56
  • Depending on what the object contains they do different things, worst case seems to be doing 1 for loop through all the elements. Commented Oct 31, 2011 at 17:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.