Skip to content

Instantly share code, notes, and snippets.

View marcosmapf's full-sized avatar

Marcos de Andrade marcosmapf

View GitHub Profile
@marcosmapf
marcosmapf / functionTimer.js
Last active April 11, 2019 04:25
Curried Function Timer to calculate function performance
/*
Accepts a function, the function arguments and the
number of attempts and calculates the average runtime
*/
const functionTimer = fn => (...args) => (attempts = 10000) => {
if (!Number(attempts) > 0) return;
let t0, t1, functionResult, timeResult, timesStamps = Array(attempts);
for (let i = 0; i < attempts; i++){
t0 = performance.now();
@marcosmapf
marcosmapf / flatSum.js
Last active August 14, 2019 06:32
Sums the input after flattening. Accepts as arguments individual values or an array
export const flatSum = (...args) => {
const isFlatArray = array => !array.some(currentValue => Array.isArray(currentValue))
const flattenArray = array => array.flat()
while (!isFlatArray(args)) {
args = flattenArray(args)
}
return args.reduce((sum, current) => sum + current)
}