for in delete vs entries filter

Benchmark created on


Setup

const o = {
	a: 123,
	b: undefined,
	c: 123,
	d: undefined
}

function filterUndefined_delete_in(obj) {
  for (const key in obj) {
    if (obj[key] === undefined) delete obj[key];
  }
  return obj
}

function filterUndefined_delete_of_keys(obj) {
  for (const key of Object.keys(obj)) {
    if (obj[key] === undefined) delete obj[key];
  }
  return obj
}

function filterUndefined_delete_of_entries(obj) {
  for (const key of Object.keys(obj)) {
    if (obj[key] === undefined) delete obj[key];
  }
  return obj
}

function filterUndefined_filter_entries(obj) {
  // $FlowIgnore
  return Object.fromEntries(
    // $FlowIgnore
    Object.entries(obj).filter(([_k, v]) => v !== undefined),
  );
}

Test runner

Ready to run.

Testing in
TestOps/sec
for in delete
filterUndefined_delete_in(o)
ready
filter entries
filterUndefined_filter_entries(o)
ready
for of keys
filterUndefined_delete_of_keys(o)
ready
for of entries
filterUndefined_delete_of_entries(o)
ready

Revisions

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