Object is empty in JavaScript

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Using Object.keys()
function isEmptyObject(obj) {
  return Object.keys(obj).length === 0;
}

// Example usage
const obj = {};
console.log(isEmptyObject(obj)); // true

const obj2 = { a: 1 };
console.log(isEmptyObject(obj2)); // false
ready
Using for...in loop
function isEmptyObject(obj) {
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      return false;
    }
  }
  return true;
}

// Example usage
const obj = {};
console.log(isEmptyObject(obj)); // true

const obj2 = { a: 1 };
console.log(isEmptyObject(obj2)); // false
ready
Using JSON.stringify
function isEmptyObject(obj) {
  return JSON.stringify(obj) === '{}';
}

// Example usage
const obj = {};
console.log(isEmptyObject(obj)); // true

const obj2 = { a: 1 };
console.log(isEmptyObject(obj2)); // false
ready
Using Object.entries()
function isEmptyObject(obj) {
  return Object.entries(obj).length === 0;
}

// Example usage
const obj = {};
console.log(isEmptyObject(obj)); // true

const obj2 = { a: 1 };
console.log(isEmptyObject(obj2)); // false
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.