
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Comparing forEach() and reduce() for summing an array of numbers in JavaScript.
We are required to compare the time taken respectively by the ES6 functions forEach() and reduce() for summing a huge array of numbers.
As we can't have a huge array of numbers here, we will simulate the magnitude of array by performing the summing operation for large number of times (iterations)
Example
Let's write the code for this −
const arr = [1, 4, 4, 54, 56, 54, 2, 23, 6, 54, 65, 65]; const reduceSum = arr => arr.reduce((acc, val) => acc + val); const forEachSum = arr => { let sum = 0; arr.forEach(el => sum += el); return sum; }; const iterations = 1000000000; console.time('reduce'); for(let i = 0; i < iterations; i++){ let sumReduce = reduceSum(arr); }; console.timeEnd('reduce'); console.time('forEach'); for(let j = 0; j < iterations; j++){ let sumForEach = forEachSum(arr); }; console.timeEnd('forEach');
Output
Following is the output in the console −
reduce: 19.058s forEach: 45.204s
So roughly, the ratio of time taken by Array.prototype.reduce() to the time taken by Array.prototype.forEach is 1 : 1.4
- Related Articles
- Summing array of string numbers using JavaScript
- JavaScript - summing numbers from strings nested in array
- Modified version of summing an array with recursion in JavaScript
- Summing all the unique values of an array - JavaScript
- Reduce an array to groups in JavaScript
- Calculating variance for an array of numbers in JavaScript
- Summing numbers from a string - JavaScript
- Combine unique items of an array of arrays while summing values - JavaScript
- Summing up unique array values in JavaScript
- Summing cubes of natural numbers within a range in JavaScript
- Reduce array in JavaScript
- Comparing integers by taking two numbers in JavaScript
- Array sum: Comparing recursion vs for loop vs ES6 methods in JavaScript
- Split an array of numbers and push positive numbers to JavaScript array and negative numbers to another?
- Reduce an array to the sum of every nth element - JavaScript

Advertisements