From 5c2e81cfd59452f01493e4e634fc2b0149e2ff28 Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 14 Feb 2018 22:38:45 +1100 Subject: [PATCH 1/3] Add mostPerformant snippet --- snippets/mostPerformant.md | 33 +++++++++++++++++++++++++++++++++ tag_database | 1 + 2 files changed, 34 insertions(+) create mode 100644 snippets/mostPerformant.md diff --git a/snippets/mostPerformant.md b/snippets/mostPerformant.md new file mode 100644 index 000000000..192c85a90 --- /dev/null +++ b/snippets/mostPerformant.md @@ -0,0 +1,33 @@ +### mostPerformant + +Returns the index of the function in an array of functions which executed the fastest. + +Use `Array.map()` to generate an array where each value is the total time taken to execute the function +after `iterations` times. Use the difference in `performance.now()` values before and after to get the total time +in milliseconds to a high degree of accuracy. +Use `Math.min()` to find the minimum execution time, and return the index of that shortest time which +corresponds to the index of the most performant function. Omit the second argument, `iterations`, to use a default of 10,000 iterations. The more iterations, the more reliable the result but the longer it will take. + +```js +const mostPerformant = (fns, iterations = 10000) => { + const times = fns.map(fn => { + const before = performance.now(); + for (let i = 0; i < iterations; i++) fn(); + return performance.now() - before; + }); + return times.indexOf(Math.min(...times)); +}; +``` + +```js +mostPerformant([ + () => { + // Loops through the entire array before returning `false` + [1, 2, 3, 4, 5, 6, 7, 8, 9, '10'].every(el => typeof el === 'number') + }, + () => { + // Only needs to reach index `1` before returning false + [1, '2', 3, 4, 5, 6, 7, 8, 9, 10].every(el => typeof el === 'number') + } +]); // 1 +``` diff --git a/tag_database b/tag_database index 09ba68764..649d26e2f 100644 --- a/tag_database +++ b/tag_database @@ -158,6 +158,7 @@ memoize:function merge:object,array minBy:math,array,function minN:array,math +mostPerformant:function negate:function nthArg:utility,function nthElement:array From 4e314dd1ffc3bae8f6c3b4cbb4c1bcaebee0a1da Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 14 Feb 2018 13:45:47 +0200 Subject: [PATCH 2/3] Update mostPerformant.md --- snippets/mostPerformant.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/snippets/mostPerformant.md b/snippets/mostPerformant.md index 192c85a90..2185b7a29 100644 --- a/snippets/mostPerformant.md +++ b/snippets/mostPerformant.md @@ -2,11 +2,9 @@ Returns the index of the function in an array of functions which executed the fastest. -Use `Array.map()` to generate an array where each value is the total time taken to execute the function -after `iterations` times. Use the difference in `performance.now()` values before and after to get the total time -in milliseconds to a high degree of accuracy. -Use `Math.min()` to find the minimum execution time, and return the index of that shortest time which -corresponds to the index of the most performant function. Omit the second argument, `iterations`, to use a default of 10,000 iterations. The more iterations, the more reliable the result but the longer it will take. +Use `Array.map()` to generate an array where each value is the total time taken to execute the function after `iterations` times. Use the difference in `performance.now()` values before and after to get the total time in milliseconds to a high degree of accuracy. +Use `Math.min()` to find the minimum execution time, and return the index of that shortest time which corresponds to the index of the most performant function. +Omit the second argument, `iterations`, to use a default of 10,000 iterations. The more iterations, the more reliable the result but the longer it will take. ```js const mostPerformant = (fns, iterations = 10000) => { From f2e775457c435148da0818475ecc3a395c762eef Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 14 Feb 2018 13:46:23 +0200 Subject: [PATCH 3/3] Update tag_database --- tag_database | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tag_database b/tag_database index 649d26e2f..af6cb99ff 100644 --- a/tag_database +++ b/tag_database @@ -158,7 +158,7 @@ memoize:function merge:object,array minBy:math,array,function minN:array,math -mostPerformant:function +mostPerformant:utility,function negate:function nthArg:utility,function nthElement:array