Simple process vs simple function

Benchmark created on


Setup

const d1 = {
	a: [1, 2, 3],
	b: [3, 2, 1]
}
const d2 = {
	a: [4, 5, 6],
	b: [4, 5, 6]
}

Test runner

Ready to run.

Testing in
TestOps/sec
Forloop
const resultForLoop = {};
const keysToProcessForLoop = ["a", "b"];

for (const key of keysToProcessForLoop) {
  if (d1.hasOwnProperty(key) && d2.hasOwnProperty(key) && Array.isArray(d1[key]) && Array.isArray(d2[key])) {
    const sumArray = d1[key].map((value, index) => value + d2[key][index]);
    resultForLoop[key] = sumArray;
  }
}
ready
Function to Handle
function addCorrespondingArraysForKeys(obj1, obj2, keys) {
  const result = {};
  for (const key of keys) {
    if (obj1.hasOwnProperty(key) && obj2.hasOwnProperty(key) && Array.isArray(obj1[key]) && Array.isArray(obj2[key])) {
      const sumArray = obj1[key].map((value, index) => value + obj2[key][index]);
      result[key] = sumArray;
    }
  }
  return result;
}

const keysToProcessFunction = ["a", "b"];
const resultFunction = addCorrespondingArraysForKeys(d1, d2, keysToProcessFunction);
ready

Revisions

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