From ce77e2b8b79c659966dcf3f2fa736f6f11324280 Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Wed, 14 Feb 2018 11:55:14 +0000 Subject: [PATCH] Travis build: 1676 --- README.md | 41 ++++++++++++++++++++++++++++++++++++++ docs/index.html | 20 ++++++++++++++++++- snippets/mostPerformant.md | 4 ++-- 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a258307c9..223fd8bbd 100644 --- a/README.md +++ b/README.md @@ -434,6 +434,7 @@ average(1, 2, 3); * [`hexToRGB`](#hextorgb-) * [`httpGet`](#httpget) * [`httpPost`](#httppost) +* [`mostPerformant`](#mostperformant) * [`nthArg`](#ntharg) * [`parseCookie`](#parsecookie) * [`prettyBytes`](#prettybytes) @@ -8044,6 +8045,46 @@ Logs: {
[⬆ Back to top](#table-of-contents) +### 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)); +}; +``` + +
+Examples + +```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 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### nthArg Creates a function that gets the argument at index `n`. If `n` is negative, the nth argument from the end is returned. diff --git a/docs/index.html b/docs/index.html index 62c8251c6..e56217194 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
@@ -1883,6 +1883,24 @@ Logs: {
   "id": 101
 }
 */
+

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.

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));
+};
+
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
 

nthArg

Creates a function that gets the argument at index n. If n is negative, the nth argument from the end is returned.

Use Array.slice() to get the desired argument at index n.

const nthArg = n => (...args) => args.slice(n)[0];
 
const third = nthArg(2);
 third(1, 2, 3); // 3
diff --git a/snippets/mostPerformant.md b/snippets/mostPerformant.md
index 2185b7a29..66cd591e2 100644
--- a/snippets/mostPerformant.md
+++ b/snippets/mostPerformant.md
@@ -21,11 +21,11 @@ const mostPerformant = (fns, iterations = 10000) => {
 mostPerformant([
   () => {
     // Loops through the entire array before returning `false`
-    [1, 2, 3, 4, 5, 6, 7, 8, 9, '10'].every(el => typeof el === 'number')
+    [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, '2', 3, 4, 5, 6, 7, 8, 9, 10].every(el => typeof el === 'number');
   }
 ]); // 1
 ```