From 5403a76a7c09cfde18dbbdd10ceb1b6a7b4dd3da Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 11 Apr 2018 23:39:49 +1000 Subject: [PATCH 1/4] Create hz.md --- snippets/hz.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 snippets/hz.md diff --git a/snippets/hz.md b/snippets/hz.md new file mode 100644 index 000000000..b05f18af6 --- /dev/null +++ b/snippets/hz.md @@ -0,0 +1,34 @@ +### hz + +Returns the number of times a function executed per second. +`hz` is the unit for `hertz`, the unit of frequency defined as one cycle per second. + +Use `window.performance.now()` before and after the iteration loop to get the difference in milliseconds +to calculate the time elapsed executing the function `iterations` times, +using math to calculate the number of iterations per second. +Omit the second argument, `iterations`, to use the default of 100 iterations. + +```js +const hz = (fn, iterations = 100) => { + const before = performance.now(); + for (let i = 0; i < iterations; i++) fn(); + return 1000 * iterations / (performance.now() - before); +}; +``` + +```js +// 10,000 element array +const numbers = Array(10000).fill().map((_, i) => i); + +// Test functions with the same goal: sum up the elements in the array +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 +}; + +// `sumForLoop` is nearly 10 times faster +Math.round(hz(sumReduce)); // 572 +Math.round(hz(sumForLoop)); // 4784 +``` From 632c565431d8e1132751efc7d4c92815314297e8 Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 11 Apr 2018 23:41:22 +1000 Subject: [PATCH 2/4] Update tag_database --- tag_database | 1 + 1 file changed, 1 insertion(+) diff --git a/tag_database b/tag_database index 24cf93bef..f086f5c62 100644 --- a/tag_database +++ b/tag_database @@ -103,6 +103,7 @@ hide:browser,css httpGet:utility,url,browser httpPost:utility,url,browser httpsRedirect:browser,url +hz:function indexOfAll:array initial:array initialize2DArray:array From 48b5a747668065719ee57c637de493d016bcf55d Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 12 Apr 2018 15:26:07 +1000 Subject: [PATCH 3/4] Update hz.md --- snippets/hz.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/snippets/hz.md b/snippets/hz.md index b05f18af6..1487b9b46 100644 --- a/snippets/hz.md +++ b/snippets/hz.md @@ -3,10 +3,7 @@ Returns the number of times a function executed per second. `hz` is the unit for `hertz`, the unit of frequency defined as one cycle per second. -Use `window.performance.now()` before and after the iteration loop to get the difference in milliseconds -to calculate the time elapsed executing the function `iterations` times, -using math to calculate the number of iterations per second. -Omit the second argument, `iterations`, to use the default of 100 iterations. +Use `window.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. ```js const hz = (fn, iterations = 100) => { From 503e74c8c6258598f700e27d5d10cd75d4cb1d97 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 12 Apr 2018 21:42:34 +0300 Subject: [PATCH 4/4] Update hz.md --- snippets/hz.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/snippets/hz.md b/snippets/hz.md index 1487b9b46..a9848fb55 100644 --- a/snippets/hz.md +++ b/snippets/hz.md @@ -3,7 +3,9 @@ Returns the number of times a function executed per second. `hz` is the unit for `hertz`, the unit of frequency defined as one cycle per second. -Use `window.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. +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. ```js const hz = (fn, iterations = 100) => {