123 (v2)

Revision 2 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_ARRAY = Object.values(ROLES);

Test runner

Ready to run.

Testing in
TestOps/sec
01
const ROLES_BINARY_SHIFT = Object.values(ROLES).reduce((roles, role, index) => {
  roles[role] = index;
  return roles;
}, { });

const getBinRole = role => 0b0000_0001 << ROLES_BINARY_SHIFT[role];

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

hasRole('introducer');
hasRole('introducer1');
ready
02
const getBinRole = roleName => {
  const index = ROLES_ARRAY.indexOf(roleName);
  return index !== -1 ? 0b0000_0001 << index : 0;
 };

const hasRole = (roleName) => {
  const binRole = getBinRole(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.