You can use a for…in loop with an Object.hasOwn (ECMA 2022+) test to check whether an object has any own properties:
Copyfunction isEmpty(obj) {
for (const prop in obj) {
if (Object.hasOwn(obj, prop)) {
return false;
}
}
return true;
}
If you also need to distinguish {}-like empty objects from other objects with no own properties (e.g. Dates), you can do various (and unfortunately need-specific) type checks:
Copyfunction isEmptyObject(value) {
if (value == null) {
// null or undefined
return false;
}
if (typeof value !== 'object') {
// boolean, number, string, function, etc.
return false;
}
const proto = Object.getPrototypeOf(value);
// consider `Object.create(null)`, commonly used as a safe map
// before `Map` support, an empty object as well as `{}`
if (proto !== null && proto !== Object.prototype) {
return false;
}
return isEmpty(value);
}
Note that comparing against Object.prototype like in this example will fail to recognize cross-realm objects.
Do not use Object.keys(obj).length. It is O(N) complexity because it creates an array containing all the property names only to get the length of that array. Iterating over the object accomplishes the same goal but is O(1).
For compatibility with JavaScript engines that don’t support ES 2022+, const can be replaced with var and Object.hasOwn with Object.prototype.hasOwnProperty.call:
Copyfunction isEmpty(obj) {
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
return false;
}
}
return true
}
Many popular libraries also provide functions to check for empty objects:
jQuery:
CopyjQuery.isEmptyObject({}); // true
lodash:
Copy_.isEmpty({}); // true
Underscore:
Copy_.isEmpty({}); // true
Hoek:
CopyHoek.deepEqual({}, {}); // true
ExtJS:
CopyExt.Object.isEmpty({}); // true
AngularJS (version 1):
Copyangular.equals({}, {}); // true
Ramda:
CopyR.isEmpty({}); // true
You can use a for…in loop with an Object.hasOwn (ECMA 2022+) test to check whether an object has any own properties:
Copyfunction isEmpty(obj) {
for (const prop in obj) {
if (Object.hasOwn(obj, prop)) {
return false;
}
}
return true;
}
If you also need to distinguish {}-like empty objects from other objects with no own properties (e.g. Dates), you can do various (and unfortunately need-specific) type checks:
Copyfunction isEmptyObject(value) {
if (value == null) {
// null or undefined
return false;
}
if (typeof value !== 'object') {
// boolean, number, string, function, etc.
return false;
}
const proto = Object.getPrototypeOf(value);
// consider `Object.create(null)`, commonly used as a safe map
// before `Map` support, an empty object as well as `{}`
if (proto !== null && proto !== Object.prototype) {
return false;
}
return isEmpty(value);
}
Note that comparing against Object.prototype like in this example will fail to recognize cross-realm objects.
Do not use Object.keys(obj).length. It is O(N) complexity because it creates an array containing all the property names only to get the length of that array. Iterating over the object accomplishes the same goal but is O(1).
For compatibility with JavaScript engines that don’t support ES 2022+, const can be replaced with var and Object.hasOwn with Object.prototype.hasOwnProperty.call:
Copyfunction isEmpty(obj) {
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
return false;
}
}
return true
}
Many popular libraries also provide functions to check for empty objects:
jQuery:
CopyjQuery.isEmptyObject({}); // true
lodash:
Copy_.isEmpty({}); // true
Underscore:
Copy_.isEmpty({}); // true
Hoek:
CopyHoek.deepEqual({}, {}); // true
ExtJS:
CopyExt.Object.isEmpty({}); // true
AngularJS (version 1):
Copyangular.equals({}, {}); // true
Ramda:
CopyR.isEmpty({}); // true
If ECMAScript 5 support is available, you can use Object.keys():
Copyfunction isEmpty(obj) {
return Object.keys(obj).length === 0;
}
For ES3 and older, there's no easy way to do this. You'll have to loop over the properties explicitly:
Copyfunction isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}
why there isn't a method to check an object is empty or not?
How can I verify if a JavaScript object is empty?
I often find myself writing Object.keys(someObject) > 0 to test if an object isn't {} (empty) there must be a more beautiful way.
What is the best method to check if a variable is not null or empty?
Are there any performance considerations when using JSON.stringify() to check if an object is empty?
What is the fastest way to check if an object is empty in React?
Can JSON.stringify() handle circular references when checking for empty objects?
Videos
in my project, an object will take properties and values based on fetched data, if that's not empty something will be done.
so I guess this solution to my problem isn't a javascript way of slowing a problem. why?