Measures the number of times a function is executed per second (`hz`

/`hertz`

).

- Use
`performance.now()`

to get the difference in milliseconds before and after the iteration loop to calculate the time elapsed executing the function`iterations`

times. - Return the number of cycles per second by converting milliseconds to seconds and dividing it by the time elapsed.
- Omit the second argument,
`iterations`

, to use the default of 100 iterations.

const hz = (fn, iterations = 100) => { const before = performance.now(); for (let i = 0; i < iterations; i++) fn(); return (1000 * iterations) / (performance.now() - before); };

##### Examples

const numbers = Array(10000).fill().map((_, i) => i); const sumReduce = () => numbers.reduce((acc, n) => acc + n, 0); const sumForLoop = () => { let sum = 0; for (let i = 0; i < numbers.length; i++) sum += numbers[i]; return sum; }; Math.round(hz(sumReduce)); // 572 Math.round(hz(sumForLoop)); // 4784

## Recommended snippets

### timeTaken

JavaScript, Function

Measures the time it takes for a function to execute.

### debouncePromise

JavaScript, Function

Creates a debounced function that returns a promise, but delays invoking the provided function until at least

`ms`

milliseconds have elapsed since the last time it was invoked. All promises returned during this time will return the same data.### mostPerformant

JavaScript, Function

Returns the index of the function in an array of functions which executed the fastest.