How to efficiently count the number of keys/properties of an object in JavaScript

To do this in any ES5-compatible environment, such as Node.js, Chrome, Internet Explorer 9+, Firefox 4+, or Safari 5+:

Object.keys(obj).length
  • Browser compatibility
  • Object.keys documentation (includes a method you can add to non-ES5 browsers)

Leave a Comment