How to Count the Number if Keys/Properties of a JavaScript object

One of the most common issues of programming with JavaScript is how to count the number of keys/properties of an object.

This short tutorial represents several efficient ways to do it.

Solution 1

The fastest way of counting the keys/properties of an object is to use the following code:

Javascript count the number if Keys/Properties
let myobj = { siteName: 'W3Docs', bookName: 'Javascript', }; let count = 0; for (key in myobj){ if (myobj.hasOwnProperty(key)) { count++; } } console.log(count);

Solution 2

Another efficient solution is to use the following code:

Object.keys(obj).length
Javascript keys length of an object
let myobj = { siteName: 'W3Docs', bookName: 'Javascript', }; console.log(Object.keys(myobj).length);

This code works internally iterating over the keys for computing a temporary array. It also returns its length.

The main advantage of this code is that it is clean and readable. But there can be a memory overhead due to the creation of an array.

Solution 3

The next solution is optimizing a for-loop. The slowest part of a for-loop is the .hasOwnProperty() call, due to the function overhead.

So, whenever you want to get the number of entries of a JSON object, you can just skip the .hasOwnProperty() call when you know there is nothing to extend the Object.prototype.

Otherwise, the code might be slightly optimized by transforming key local (var key)and applying the prefix-increment operator (++count)instead of postfix.

Let’s demonstrate it in the example below:

Javascript hasOwnProperty
let myobj = { siteName: 'W3Docs', bookName: 'Javascript', }; let count = 0; for (let key in myobj){ if (myobj.hasOwnProperty(key)) { ++count; } } console.log(count);

There is another option, which relies on catching the hasOwnProperty method, as follows:

Javascript hasOwnProperty
let myobj = { siteName: 'W3Docs', bookName: 'Javascript', }; let hasOwn = Object.prototype.hasOwnProperty; let count = 0; for (let key in myobj) { if (hasOwn.call(myobj, key)) { ++count; } } console.log(count);

And, the final solution to counting the number of keys/properties of an object in JavaScript is the following:

Javascript keys/properties of an object
let myObj = new Object(); Object.defineProperty(myObj, "nonEnumerableProp", { enumerable: false }); Object.defineProperty(myObj, "enumerableProp", { enumerable: true }); console.log(Object.getOwnPropertyNames(myObj).length); //outputs 2 console.log(Object.keys(myObj).length); //outputs 1 console.log(myObj.hasOwnProperty("nonEnumerableProp")); //outputs true console.log(myObj.hasOwnProperty("enumerableProp")); //outputs true console.log("nonEnumerableProp" in myObj); //outputs true console.log("enumerableProp" in myObj); //outputs true

Defining Objects in JavaScript

JavaScript objects are generally implemented for storing keyed collections of different data and more complex entities.

In JavaScript, objects are involved in all the components of the language; hence you need to understand them as soon as you start to study it. Objects are generated with figure brackets {…} and should have a list of properties. The property is a “key: value”, in which key or property name is a string and value can be any.