for in vs Object.keys

Benchmark created on


Setup

const old = {
      1: ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'],
      2: ['D', 'G'],
      3: ['B', 'C', 'M', 'P'],
      4: ['F', 'H', 'V', 'W', 'Y'],
      5: ['K'],
      8: ['J', 'X'],
      10: ['Q', 'Z'],
    };

Test runner

Ready to run.

Testing in
TestOps/sec
for in
const transform = (values) => {
  const transformed = {}
  for (const value in values) {
    const numeric = Number(value);
    for (const letter of values[value]) {
      transformed[letter.toLowerCase()] = numeric;
    };
  };
  return transformed;
};

transform(old);
ready
Object.keys
function transform(legacyScores) {
  const scores = {};
  Object.keys(legacyScores)
    .forEach((points) => {
  	  const numeric = Number(points);
      legacyScores[points].forEach((letter) => {
        scores[letter.toLowerCase()] = numeric ;
      });
    });
  return scores;
}


transform(old);
ready

Revisions

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