Checking if an object is all null or undefined (v3)

Revision 3 of this benchmark created on


Setup

const objectWithAllNullish = {
  'A': null,
  'B': undefined,
  'C': null,
  'D': undefined,
  'E': null,
  'F': undefined,
  'G': null,
  'H': undefined,
  'I': null,
  'J': undefined,
  'K': null,
  'L': undefined,
  'M': null,
  'N': undefined,
  'O': null,
  'P': undefined,
  'Q': null,
  'R': undefined,
  'S': null,
  'T': undefined,
  'U': null,
  'V': undefined,
  'W': null,
  'X': undefined,
  'Y': null,
  'Z': undefined,
};

const objectWithEarlyNonNullish = {
  'A': 'not null',
  'B': undefined,
  'C': null,
  'D': undefined,
  'E': null,
  'F': undefined,
  'G': null,
  'H': undefined,
  'I': null,
  'J': undefined,
  'K': null,
  'L': undefined,
  'M': null,
  'N': undefined,
  'O': null,
  'P': undefined,
  'Q': null,
  'R': undefined,
  'S': null,
  'T': undefined,
  'U': null,
  'V': undefined,
  'W': null,
  'X': undefined,
  'Y': null,
  'Z': undefined,
};

const objectWithLateNonNullish = {
  'A': null,
  'B': undefined,
  'C': null,
  'D': undefined,
  'E': null,
  'F': undefined,
  'G': null,
  'H': undefined,
  'I': null,
  'J': undefined,
  'K': null,
  'L': undefined,
  'M': null,
  'N': undefined,
  'O': null,
  'P': undefined,
  'Q': null,
  'R': undefined,
  'S': null,
  'T': undefined,
  'U': null,
  'V': undefined,
  'W': null,
  'X': undefined,
  'Y': null,
  'Z': 'not undefined',
};

Test runner

Ready to run.

Testing in
TestOps/sec
Every
const areAllValuesNull = (obj) => {
    const values = Object.values(obj);
    return values.every((value) => value === null || value === undefined);
};


areAllValuesNull(objectWithAllNullish)
areAllValuesNull(objectWithEarlyNonNullish)
areAllValuesNull(objectWithLateNonNullish)
ready
Some
const areAnyValuesNotNullish = (obj) => {
    const values = Object.values(obj);
    return values.some((value) => value !== null && value !== undefined);
};


areAnyValuesNotNullish(objectWithAllNullish)
areAnyValuesNotNullish(objectWithEarlyNonNullish)
areAnyValuesNotNullish(objectWithLateNonNullish)
ready
Simpler Callback Every
const areAllValuesNull = (obj) => {
    const values = Object.values(obj);
    return values.every((value) => value == null);
};


areAllValuesNull(objectWithAllNullish)
areAllValuesNull(objectWithEarlyNonNullish)
areAllValuesNull(objectWithLateNonNullish)
ready
Simpler Callback Some
const areAnyValuesNotNullish = (obj) => {
    const values = Object.values(obj);
    return values.some((value) => value != null);
};


areAnyValuesNotNullish(objectWithAllNullish)
areAnyValuesNotNullish(objectWithEarlyNonNullish)
areAnyValuesNotNullish(objectWithLateNonNullish)
ready

Revisions

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