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
AI summary
To check if an object is empty in JavaScript, the function isObjectEmpty iterates through the object's properties. If any properties exist, it returns false; otherwise, it returns true. This method is one of the fastest ways to determine if an object is empty.

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 !