123 (v3)

Revision 3 of this benchmark created on


Setup

const user = {
  _ROLES_BIN: 30,
  _ROLES_RAW: [
    { id: 4, name: 'broker', type: 'broker' },
    { id: 5, name: 'introducer', type: 'introducer' },
    { id: 7, name: 'super_broker', type: 'super_broker' },
    { id: 8, name: 'broker_ops', type: 'broker_ops' },
  ],
};

const ROLES = {
  Client: 'client',
  Introducer: 'introducer',
  Broker: 'broker',
  BrokerOps: 'broker_ops',
  SuperBroker: 'super_broker',
};

const ROLES_BINARY_SHIFT1 = Object.values(ROLES).reduce((roles, role, index) => {
  roles[role] = index;
  return roles;
}, { });

const ROLES_BINARY_SHIFT2 = Object.values(ROLES).reduce((acc, role, index) => ({ ...acc, [role]: 0b0000_0001 << index }), {});

Test runner

Ready to run.

Testing in
TestOps/sec
01
const getBinRole = role => 0b0000_0001 << ROLES_BINARY_SHIFT1[role];

const hasRole = (roleName) => {
  const binRole = getBinRole(roleName);
  return (binRole & user._ROLES_BIN) > 0;
};

hasRole('introducer');
hasRole('introducer1');
ready
02
const hasRole = (roleName) => {
  const binRole = ROLES_BINARY_SHIFT2[roleName];
  return (binRole & user._ROLES_BIN) > 0;
};

hasRole('introducer');
hasRole('introducer1');
ready

Revisions

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