I read the article below suggesting to use the built-in array functions in JavaScript. I’ve always heard that those methods are significantly slower than the built-in for loop, but I never actually measured it myself.

Simplify your JavaScript — Use .map(), .reduce(), and .filter()

If you haven’t heard of .map(), .reduce(), and .filter() in JavaScript, you might want to learn to use it!

medium.com

So, I figured now is as good of a time as any. So, let’s go run some tests!


Baselines

The machine I’m running the code is a MacBook Pro with a 2.5GHz Quad-Core Intel Core i7 processor, and 16GB 1600 MHz DDR3 memory. My node version is 12.14.0.

While the max array length is ²³², my computer actually cannot create an array with that length without a JavaScript heap out of memory error. Based on local testing, 50 million length array is as high as I am able to go.


Criteria

These functions will be judged in two ways:

  • memory usage — measured using process.memoryUsage().heapUsed
  • execution speed — measured using the Date object and first-grade math

#javascript #javascript-tips #functional-programming #javascript-development #arrays

Should you use .map(), .reduce(), or .filter()? Are they fast enough?
4.65 GEEK