123

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
01
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_SHIFT = Object.values(ROLES).reduce((roles, role, index) => {
  roles[role] = index;
  return roles;
}, { });

// Use arrow function for conciseness
const getBinRole = role => 0b0000_0001 << ROLES_BINARY_SHIFT[role];

// Use arrow function and destructuring for conciseness
const hasRole = (roleName) => {
  const binRole = getBinRole(roleName);
  return (binRole & user._ROLES_BIN) > 0;
};

hasRole('introducer');
ready
02
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' }
 ],
};

// Convert ROLES to an array
const ROLES = ['client', 'introducer', 'broker', 'broker_ops', 'super_broker'];

// Function to get binary role using the index of the role name in the ROLES array
const getBinRole = roleName => {
 const index = ROLES.indexOf(roleName);
 return index !== -1 ? 0b0000_0001 << index : 0;
};

// Function to check if the user has a specific role
const hasRole = roleName => {
 const binRole = getBinRole(roleName);
 return (binRole & user._ROLES_BIN) > 0;
};

hasRole('introducer');
ready

Revisions

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