How to check if an object is empty in javascript

How to check if an object is empty in javascript

There are a few ways to check if an object is empty in Javascript.
But I care about performance and readability.

The fastest way is to use a loop.
You could use the below code snippet as a utility function.

function isEmpty(obj) {
    for(let key in obj) {
            return false;
    return true;
Fastest check for an empty object

My favorite way for readability reasons and it's still high performing is using Object.keys.

function isEmpty(obj) {
    return Object.keys(obj).length === 0
Easy to remember snippet for an empty object

Note: Object.keys is faster than using Object.entries.

The statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.
The Object.keys() method returns an array of a given object’s own enumerable property names, iterated in the same order that a normal loop would.
Show Comments