Fastest way to check if an object is empty

Fastest way to check if an object is empty

Tags
Computer Science
React.js
JavsScript
Published
August 27, 2023

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.
/** * 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 }
 
Happy Coding !