DEV Community

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

Collapse
 
somedood profile image
Basti Ortiz • Edited

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);
}