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: w
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.