Filter object entries by property and remove property (v2)

Revision 2 of this benchmark created on


Setup

const ERROR = {
  EXDEV: {
    CODE: 'EXDEV',
    DESC: 'Invalid cross-device link (POSIX.1-2001).',
    ID: -18,
    IS_NODE: true,
    IS_POSIX: true
  },
  EXFULL: {
    CODE: 'EXFULL',
    DESC: 'Exchange full.',
    ID: -54,
    IS_NODE: false,
    IS_POSIX: true
  },
  UNKNOWN: {
    CODE: 'UNKNOWN',
    DESC: 'Unknown error.',
    ID: -4094,
    IS_NODE: true,
    IS_POSIX: false
  }
}

Test runner

Ready to run.

Testing in
TestOps/sec
`Array.prototype.reduce` and spread
Object.entries(ERROR)
  .filter(([, value]) => value.IS_NODE)
  .reduce((accum, [key, value]) => {
    const { IS_NODE, IS_POSIX, ...spread } = value;

    return accum[key] = spread, accum
  }, {})
ready
`Array.prototype.reduce` and create
Object.entries(ERROR)
  .filter(([, value]) => value.IS_NODE)
  .reduce((accum, [key, value]) => {
    const { CODE, DESC, ID } = value;

    return { ...accum, [key]: { CODE, DESC, ID } }
  }, {})
ready
`Object.fromEntries` and spread
Object.fromEntries(
  Object.entries(ERROR)
    .filter(([, value]) => value.IS_NODE)
    .map(([key, value]) => {
      const { IS_NODE, IS_POSIX, ...spread } = value;

      return [key, spread]
    })
)
ready
`Object.fromEntries`and create
Object.fromEntries(
  Object.entries(ERROR)
    .filter(([, value]) => value.IS_NODE)
    .map(([key, value]) => {
      const { CODE, DESC, ID } = value;

      return [key, { CODE, DESC, ID }]
    })
)
ready
`Object.fromEntries`and delete
Object.fromEntries(
  Object.entries(ERROR)
    .filter(([, value]) => value.IS_NODE)
    .map(([key, value]) => 
      (
        delete value['IS_NODE'],
        delete value['IS_POSIX'],
        [key, value]
      )
    )
)
ready

Revisions

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