DEV Community

Discussion on: A Pedant's Experiment on Optimizing Callback Functions for Array Iteration

Collapse
mpetito profile image
Michael Petito

Is the script under "Methodology" exactly what was run to produce these results? If so, you may want to re-run with the following modifications and see if there are any differences in the results:

  1. Use only a single approach in each rest run. Running all 4 approaches in each iteration of the loop could result in different runtime optimizations and memory access patterns vs. running each approach separately.

  2. For each approach and iteration, run the test several times. Note the execution times for first, min, max and avg. I'm guessing you'd want to know best of avg performance.

Performance testing is hard and good methodology really is key to drawing any reliable conclusions.

I definitely agree though, "these types of optimizations generally do not have significant consequences."

Collapse
somedood profile image
Basti Ortiz Author • Edited on

Phew! I'm glad to tell you that, yes, the results are still consistent with my findings. Of course, it has some plus-and-minus here and there, but the trend still holds true.

I isolated each test case in their own .js file if you're wondering how I modified the script. I ran each respective script and plotted the results. The charts were still similar to the ones in the article.

Here's one of the script files I used for each respective test case. There are three others (for each test case), but the changes are very minor. I simply had to change the contents of the for loop to the appropriate code snippet.

// UncachedRegular.js
const { performance } = require('perf_hooks');

// Test parameters
const ORDERS_OF_MAGNITUDE = 8;

// Stored functions
function plus1Func(x) { return x + 1; }
const plus1Arrow = x => x + 1;

for (let i = 1; i < 10 ** ORDERS_OF_MAGNITUDE; i *= 10) {
  // Test array
  const test = new Array(i).fill(0, 0, i);

  // Uncached (regular function)
  const a0 = performance.now();
  test.map(function(x) { return x + 1 });
  const a1 = performance.now();
  const uncachedRegular = a1 - a0;

  // Log results here
  console.log(uncachedRegular);
}