11

I have a Javascript object that I'm trying to use as a "hashmap". The keys are always strings, so I don't think I need anything as sophisticated as what's described in this SO question. (I also don't expect the number of keys to go above about 10 so I'm not particularly concerned with lookups being O(n) vs. O(log n) etc.)

The only functionality I want that built-in Javascript objects don't seem to have, is a quick way to figure out the number of key/value pairs in the object, like what Java's Map.size returns. Of course, you could just do something like:

function getObjectSize(myObject) {
  var count=0
  for (var key in myObject)
    count++
  return count
}

but that seems kind of hacky and roundabout. Is there a "right way" to get the number of fields in the object?

Community
  • 1
  • 1
MatrixFrog
  • 20,873
  • 10
  • 56
  • 88
  • The only suggestion to your method would be to add an additional parameter to exclude/include inherited properties, in that case you would use hasOwnProperty instead of just the simple for loop – Juan Mendes Dec 08 '10 at 20:13

4 Answers4

14

There is an easier way spec'd in ECMAScript 5.

Object.keys(..) returns an array of all keys defined on the object. Length can be called on that. Try in Chrome:

Object.keys({a: 1, b: 2}).length; // 2

Note that all objects are basically key/value pairs in JavaScript, and they are also very extensible. You could extend the Object.prototype with a size method and get the count there. However, a much better solution is to create a HashMap type interface or use one of the many existing implementations out there, and define size on it. Here's one tiny implementation:

function HashMap() {}

HashMap.prototype.put = function(key, value) {
    this[key] = value;
};

HashMap.prototype.get = function(key) {
    if(typeof this[key] == 'undefined') {
        throw new ReferenceError("key is undefined");
    }
    return this[key];
};

HashMap.prototype.size = function() {
    var count = 0;

    for(var prop in this) {
        // hasOwnProperty check is important because 
        // we don't want to count properties on the prototype chain
        // such as "get", "put", "size", or others.
        if(this.hasOwnProperty(prop) {
            count++;
        }
    }

    return count;
};

Use as (example):

var map = new HashMap();
map.put(someKey, someValue);
map.size();
Anurag
  • 132,806
  • 34
  • 214
  • 257
  • keys only returns the Enumerable, hasOwnProperty keys. – Matthew Flaschen Jul 01 '10 at 01:10
  • "TypeError: Object.keys is not a function". I guess I should have mentioned that I'm working on a Firefox extension, so if it doesn't work in Firefox then it's pretty much useless to me. Still, good information for the future. Thanks! – MatrixFrog Jul 01 '10 at 01:10
  • @Matthew - yes, that's why I said *defined on the object* and yes enumerability is important too, but that's what `for..in` will return as well, so I left it out of the discussion. @MatrixFrog - It should be coming in Firefox 4, and you can always defined a custom object type with a size method, as in the above example. – Anurag Jul 01 '10 at 01:15
  • 1
    @Matthew, to get all the properties, enumerable or not, we can now use in ES5 the [`Object.getOwnPropertyNames`](http://ecma262-5.com/ELS5_HTML.htm#Section_15.2.3.4) method. – Christian C. Salvadó Jul 01 '10 at 03:55
3

A correction: you need to check myObject.hasOwnProperty(key) in each iteration, because there're can be inherited attributes. For example, if you do this before loop Object.prototype.test = 'test', test will aslo be counted.

And talking about your question: you can just define a helper function, if speed doesn't matter. After all, we define helpers for trim function and other simple things. A lot of javascript is "kind of hacky and roundabout" :)

update
Failure example, as requested.

Object.prototype.test = 'test';
var x = {};
x['a'] = 1;
x['b'] = 2;

The count returned will be 3.

Nikita Rybak
  • 64,889
  • 22
  • 150
  • 172
  • The object is created with `myObject = {}` and then properties are added to it with `myObject[key] = value` where `key` is always a string. So I think that wouldn't be a concern in this case. – MatrixFrog Jul 01 '10 at 01:12
  • @MatrixFrog Oh, really? :) It doesn't matter what type is 'key'. I added example to clarify the point. – Nikita Rybak Jul 01 '10 at 01:17
  • btw, Anurag presses the same point in his answer (the comment in there) – Nikita Rybak Jul 01 '10 at 01:19
  • I disagree with using hasOwnProperty, only use that if you only want the props on the object itself. In my case, however, I usually want to count inherited properties too. Plus, if you're using an Object as a Map, there are no inherited enumerable properties. Unless you use a naive library that modifies the "prototype" of object, in which case you should know better and have already run into this. – Juan Mendes Dec 08 '10 at 20:11
0

you could also just do myObject.length (in arrays)

nevermind, see this: JavaScript object size

Community
  • 1
  • 1
pop850
  • 3,067
  • 3
  • 26
  • 34
  • 1
    He's not asking about arrays, or for a `sizeof`. He wants the number of mappings (key-value pairs) in an object. – Matthew Flaschen Jul 01 '10 at 00:57
  • `myObject.length` was my first thought, but yes, it only works for arrays. – MatrixFrog Jul 01 '10 at 01:06
  • BTW, the link you give is for SIZEOF, e.g. number of bytes used. OP wants to know the number of key-value pairs in an object. (I almost did -1 because this is not an answer to the question; however the link is interesting, even if on a different topic.) – ToolmakerSteve Aug 01 '14 at 05:59
0

That's all you can do. Clearly, JavaScript objects are not designed for this. And this will only give you the number of Enumerable properties. Try getObjectSize(Math).

Matthew Flaschen
  • 255,933
  • 45
  • 489
  • 528