Take the 2-minute tour ×

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)
  return count

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

share|improve this question
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 8 '10 at 20:13
add comment

4 Answers

up vote 3 down vote accepted

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" :)

Failure example, as requested.

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

The count returned will be 3.

share|improve this answer
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 1 '10 at 1:12
@MatrixFrog Oh, really? :) It doesn't matter what type is 'key'. I added example to clarify the point. –  Nikita Rybak Jul 1 '10 at 1:17
btw, Anurag presses the same point in his answer (the comment in there) –  Nikita Rybak Jul 1 '10 at 1: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 8 '10 at 20:11
add comment

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) {

    return count;

Use as (example):

var map = new HashMap();
map.put(someKey, someValue);
share|improve this answer
keys only returns the Enumerable, hasOwnProperty keys. –  Matthew Flaschen Jul 1 '10 at 1: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 1 '10 at 1: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 1 '10 at 1:15
@Matthew, to get all the properties, enumerable or not, we can now use in ES5 the Object.getOwnPropertyNames method. –  CMS Jul 1 '10 at 3:55
add comment

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

nevermind, see this: http://stackoverflow.com/questions/1248302/javascript-object-size/1248343#1248343

share|improve this answer
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 1 '10 at 0:57
myObject.length was my first thought, but yes, it only works for arrays. –  MatrixFrog Jul 1 '10 at 1:06
add comment

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).

share|improve this answer
add comment

Your Answer


