Retrieve enabled keys from an object map

Benchmark created on


Description

From a object map of key->boolean returns an array of keys of the entries whose value is true

Setup

const flags = Array(1000).fill(null).reduce((acc,_,i) => {
	acc[i] = Math.floor(Math.random() * 2) ? true: false;
    return acc;
},{});

Test runner

Ready to run.

Testing in
TestOps/sec
Reduce
return Object.entries(flags).reduce((acc, [flagId, enabled]) => {
    if (enabled) {
      acc.push(+flagId);
    }
    return acc;
  }, []);
ready
JSON Stringify
return Object.keys(
    JSON.parse(
      JSON.stringify(
        flags,
        (_, enabled) => {
          if (enabled) return enabled;
        }
      )
    )
  );
ready
JSON Parse
return Object.keys(
    JSON.parse(
      JSON.stringify(flags),
      (_, enabled) => {
        if (enabled) return enabled;
      }
    )
  );
ready

Revisions

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