In Sample?

Benchmark created on


Setup

const generateHashCode = function (str) {
  let hash = 0;
  if (str.length === 0) return hash;
  for (let i = 0; i < str.length; i++) {
    const chr = str.charCodeAt(i);
    hash = (hash << 5) - hash + chr;
    hash |= 0;
  }
  return hash;
};

const isSessionInSample = function (sessionId, sampleRate) {
  const hashNumber = generateHashCode(sessionId.toString());
  const absHash = Math.abs(hashNumber);
  const absHashMultiply = absHash * 31;
  const mod = absHashMultiply % 100;
  return mod / 100 < sampleRate;
};

Test runner

Ready to run.

Testing in
TestOps/sec
Before
isSessionInSample(String(Date.now()))
ready
After
isSessionInSample(String(Date.now()))
ready

Revisions

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