Skip to content

Instantly share code, notes, and snippets.

# oanhnn/use-reduce.js

Created April 7, 2019 02:14
Show Gist options
• Save oanhnn/9390a38112670f1e74767e8d29dda20f to your computer and use it in GitHub Desktop.
Test reduce function https://evertpot.com/no-reduce/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 const callbackFunction1 = function( accumulator, currentElement, currentIndex, array ) { // Get the maximum by checking first if there is a maximum from the previous step const maximum = accumulator.maximum ? // If there is, then check if the current element is higher than the previous maximum accumulator.maximum < currentElement ? currentElement : accumulator.maximum : // If there isn't, use the current element right away currentElement; // Get the minimum by checking first if there is a minimum from the previous step const minimum = accumulator.minimum ? // If there is, then check if the current element is lower than the previous maximum accumulator.minimum > currentElement ? currentElement : accumulator.minimum : // If there isn't, use the current element right away currentElement; // Get the average by checking if we're at the last step (where it we can finally calculate the average) const average = currentIndex === array.length - 1 ? (accumulator.average + currentElement) / array.length : // If we're not at the last step, check if there even is a value from the previous step accumulator.average ? accumulator.average + currentElement : currentElement; // Return the value for the next element return { maximum, minimum, average }; }; const getArrayStats1 = function(input) { return input.reduce(callbackFunction1, {}) }; // ========================================================================================================= const callbackFunction2 = function( accumulator, currentElement, currentIndex, array ) { // Check if the current element is first element return currentIndex === 0 ? { maximum: currentElement, minimum: currentElement, sum: currentElement } : { // Check if the current element is higher than the previous maximum maximum: accumulator.maximum < currentElement ? currentElement : accumulator.maximum, // Check if the current element is lower than the previous maximum minimum: accumulator.minimum > currentElement ? currentElement : accumulator.minimum, sum: accumulator.sum + currentElement }; }; const getArrayStats2 = function(input) { if (input.length === 0) return {}; const result = input.reduce(callbackFunction2, {}); return { maximum: result.maximum, minimum: result.minimum, average: result.sum / input.length }; }; // ======================================================================================================== const getArrayStats3 = function(input) { if (input.length === 0) return {}; let maximum = input[0]; let minimum = input[0]; let sum = 0; for(const item of input) { maximum = item > maximum ? item : maximum; minimum = item < maximum ? item : minimum; sum += item; } const average = sum / input.length; return { maximum, minimum, average }; }; // ========================================================================================================= const createRandomArray = function(length, max) { return Array.from({length}, () => Math.floor(Math.random() * max)); }; // Data const data = createRandomArray(10, 10000); // Test 1 const time1 = {start: 0, end: 0, run: 0}; time1.start = process.hrtime.bigint(); const result1 = getArrayStats1(data); time1.end = process.hrtime.bigint(); time1.run = time1.end - time1.start; // Test 2 const time2 = {start: 0, end: 0, run: 0}; time2.start = process.hrtime.bigint(); const result2 = getArrayStats2(data); time2.end = process.hrtime.bigint(); time2.run = time2.end - time2.start; // Test 3 const time3 = {start: 0, end: 0, run: 0}; time3.start = process.hrtime.bigint(); const result3 = getArrayStats3(data); time3.end = process.hrtime.bigint(); time3.run = time3.end - time3.start; console.log(result1, time1); console.log(result2, time2); console.log(result3, time3); console.log(data);
to join this conversation on GitHub. Already have an account? Sign in to comment