HomeAbout MePublicationsContact

Fastest way to check if an object is empty

By Suresh Kumar Mukhiya
Published in Frontend Cheetseat
October 27, 2021
1 min read

Problem:

Given an object, the aim is to check weather the object is empty or not.

Input:

const data = {}

Expected Output:

isObjectEmpty({}) => TRUE
isObjectEmpty({a: 1}) => FALSE

Solution:

Note, there are several ways to check weather an object is EMPTY or not. The solution given below is one of the fastest way to check it.

Check benchmarking here: https://tomekkolasa.com/how-to-check-if-object-is-empty-in-javascript.

/**
 * There is one of the fastest way to check if an object is Empty
 */

export const isObjectEmpty = (myObject = {}): boolean => {
  for (const prop in myObject) {
    if (Object.prototype.hasOwnProperty.call(myObject, prop)) {
      return false
    }
  }
  return true
}

Tags

objectJavaScript
Previous Article
Concatenate array of objects insided array of objects
Suresh Kumar Mukhiya

Suresh Kumar Mukhiya

Software Engineer

Topics

Artificial Intelligence
Frontend Cheetseat
My Philosophy
Paper Review
Research Methods
Software Architecture

Related Posts

Concatenate array of objects insided array of objects
October 26, 2021
1 min
© 2021, All Rights Reserved.

Quick Links

PublicationsAbout meContact Me

Social Media