object last key access

Benchmark created on


Setup

const fiscalYears = Object.fromEntries(
  Array.from({ length: 100000 }, (_, idx) => [String(idx), `DATA-${idx}`]).map((value) => ({
    value,
    sort: Math.random(),
  })),
)

Test runner

Ready to run.

Testing in
TestOps/sec
sort
 const latestYear = [...Object.keys(fiscalYears)].sort((a, b) => +a - +b)[
    Object.keys(fiscalYears).length - 1
  ]

  const fiscalData = fiscalYears[latestYear]
ready
reduce
const [, fiscalData] = Object.entries(fiscalYears).reduce(
  (acc, current) => {
  	const parsedCurrentYear = +current[0]
  	return acc[0] > parsedCurrentYear ? acc : [parsedCurrentYear, current[1]]
  },
  [0, null],
)
ready

Revisions

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