Test cleanup code

Benchmark created on


Setup

const data = {
	valuea: 123,
	valueb: null,
	valuec: 'ads',
	valued: undefined,
	valuee: '',
	valuef: 3456,
	valueg: 123,
	valueh: null,
	valuei: 'ads',
	valuej: undefined,
	valuek: '',
	valuel: 3456
}

function cleanResponse(response, requiredFields = []) {
  return Object.fromEntries(
    Object.entries(response)
      .filter(([key, value]) => value != null || requiredFields.includes(key))
      .map(([key, value]) => [
        key,
        value && typeof value === 'object' && !Array.isArray(value) ? cleanResponse(value, requiredFields) : value,
      ])
  );
}

function cleanResponseNew(response, requiredFields = []) {
  return Object.entries(response).reduce((acc, [key, value]) => {
    if (value != null || requiredFields.includes(key)) {
      acc[key] = value && typeof value === 'object' && !Array.isArray(value) ? cleanResponse(value, requiredFields) : value;
    }
    return acc;
  }
  , {});
}

Test runner

Ready to run.

Testing in
TestOps/sec
Original
cleanResponse(data)
ready
Optimized
cleanResponseNew(data)
ready

Revisions

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