Travis build: 1676

This commit is contained in:
30secondsofcode
2018-02-14 11:55:14 +00:00
parent 7aaa55673c
commit 15d6b42d5b
3 changed files with 62 additions and 3 deletions

View File

@ -434,6 +434,7 @@ average(1, 2, 3);
* [`hexToRGB`](#hextorgb-) * [`hexToRGB`](#hextorgb-)
* [`httpGet`](#httpget) * [`httpGet`](#httpget)
* [`httpPost`](#httppost) * [`httpPost`](#httppost)
* [`mostPerformant`](#mostperformant)
* [`nthArg`](#ntharg) * [`nthArg`](#ntharg)
* [`parseCookie`](#parsecookie) * [`parseCookie`](#parsecookie)
* [`prettyBytes`](#prettybytes) * [`prettyBytes`](#prettybytes)
@ -8044,6 +8045,46 @@ Logs: {
<br>[⬆ Back to top](#table-of-contents) <br>[⬆ 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));
};
```
<details>
<summary>Examples</summary>
```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
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### nthArg ### nthArg
Creates a function that gets the argument at index `n`. If `n` is negative, the nth argument from the end is returned. Creates a function that gets the argument at index `n`. If `n` is negative, the nth argument from the end is returned.

File diff suppressed because one or more lines are too long

View File

@ -21,11 +21,11 @@ const mostPerformant = (fns, iterations = 10000) => {
mostPerformant([ mostPerformant([
() => { () => {
// Loops through the entire array before returning `false` // 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 // 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 ]); // 1
``` ```