Retrieving first integer

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Retrieving first integer using string
responseStatusCodes = {
  no_response: 0,
  info: 1,
  success: 2,
  redirect: 3,
  clientError: 4,
  serverError: 5
}

function getKeyByValue(object, value) { 
    return Object.keys(object).find(key => 
        object[key] === value); 
} 

const firstIntegerUsingString = function(integer) {
    var digit = parseInt(integer.toString()[0]);
    console.log(getKeyByValue(responseStatusCodes, digit));
    return digit;
}

for(let x = 0; x<600; x+=100) {
  console.log(firstIntegerUsingString(x));
}
ready
Retrieving first integer using int
responseStatusCodes = {
  no_response: 0,
  info: 1,
  success: 2,
  redirect: 3,
  clientError: 4,
  serverError: 5
}

function getKeyByValue(object, value) { 
    return Object.keys(object).find(key => 
        object[key] === value); 
} 

const firstIntegerUsingMath = function(integer) {
  const len = String(Math.abs(integer)).length;
  const divisor = 10 ** (len - 1);
  // 2: get integer part from result of division
  const digit = Math.trunc(integer/divisor);
  console.log(getKeyByValue(responseStatusCodes, digit));
  return digit;
} 

for(let x = 0; x<600; x+=100) {
  console.log(firstIntegerUsingMath(x));
}
 
ready

Revisions

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