Introduction
In JavaScript, determining the "size" or number of properties in an object is a common requirement. Unlike arrays, which have a built-in length
property, objects do not inherently provide such functionality. This tutorial covers various methods to accurately measure the size of a JavaScript object, including considerations for both enumerable and non-enumerable (symbolic) properties.
The Basics: Object Properties
JavaScript objects can be thought of as collections of key-value pairs where keys are strings or symbols. When we refer to the "size" of an object, we mean the count of these unique key-value pairs it contains. Unlike arrays that maintain order and have a length
property, objects do not inherently provide this information.
Using Object.keys()
The most straightforward method introduced in ECMAScript 5 (ES5) to obtain enumerable properties’ keys is Object.keys()
:
const myObject = {
firstname: "Gareth",
lastname: "Simpson",
age: 21
};
let size = Object.keys(myObject).length;
console.log(size); // Output: 3
Object.keys()
returns an array of all enumerable property names found directly upon the object. This method is widely supported in modern browsers and serves as a reliable way to determine how many enumerable properties an object has.
Handling Symbolic Properties
ES6 introduced symbolic keys, which are unique and can be used as object properties:
const person = {
[Symbol('name')]: 'John Doe',
[Symbol('age')]: 33,
occupation: "Programmer"
};
let propSymbols = Object.getOwnPropertySymbols(person);
console.log(propSymbols.length); // Output: 2
Object.keys()
does not capture symbolic properties. For these, Object.getOwnPropertySymbols()
is used to get an array of symbols associated with the object.
Comprehensive Size Calculation
To include both enumerable and symbolic properties in size calculation:
- Use
Object.keys()
for enumerable keys. - Use
Object.getOwnPropertySymbols()
for symbolic keys. - Optionally, use
Object.getOwnPropertyNames()
for all property names including non-enumerable ones (excluding symbols).
Here’s how you can implement this logic comprehensively:
function getObjectSize(obj) {
const keys = Object.keys(obj);
const symbols = Object.getOwnPropertySymbols(obj);
return keys.length + symbols.length;
}
const person = {
[Symbol('name')]: 'John Doe',
[Symbol('age')]: 33,
occupation: "Programmer"
};
console.log(getObjectSize(person)); // Output: 3
Cross-Browser Compatibility
For environments that may not support Object.keys()
or Object.getOwnPropertySymbols()
, you might consider polyfills:
if (!Object.keys) {
Object.keys = function (obj) {
let keys = [];
for (let key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
keys.push(key);
}
}
return keys;
};
}
// Use similar logic to polyfill `Object.getOwnPropertySymbols()` if necessary.
Alternative Libraries
If you are working within environments that support additional libraries like Underscore.js or jQuery, they offer utility methods:
-
Underscore.js:
const size = _.size({one: 1, two: 2, three: 3}); // Output: 3
-
jQuery Plugin (if you have jQuery):
$.assocArraySize = function(obj) { let size = 0; for (let key in obj) { if (obj.hasOwnProperty(key)) size++; } return size; }; const myObject = { a: 1, b: 2 }; console.log($.assocArraySize(myObject)); // Output: 2
Conclusion
Measuring the size of JavaScript objects involves understanding both enumerable and symbolic properties. Modern JavaScript provides robust methods such as Object.keys()
and Object.getOwnPropertySymbols()
, enabling effective object property enumeration. For legacy environments, consider polyfills or utility libraries like Underscore.js to handle these operations seamlessly.