-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpitStop.js
More file actions
29 lines (23 loc) · 775 Bytes
/
pitStop.js
File metadata and controls
29 lines (23 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// An accessory function for randomising inputs
export const randomNum = (min, max) =>
Math.floor(Math.random() * (max - min + 1)) + min;
// Record time taken for a function for a given number of and arguments iterations
export function timeTaken(fx, iterations, ...args) {
const startTime = performance.now();
for (let i = 0; i < iterations; i++) fx(...args);
const result = performance.now() - startTime;
return result < 1000 ?
result.toFixed(1) + ' ms':
(result / 1000).toFixed(1) + ' s';
}
// main function for testing,analysis and comparison
export function pitStop(iterations, args, ...functions) {
return console.table(
functions.map(fx => {
return {
name: fx.name,
time: timeTaken(fx, iterations, ...args),
output: fx(...args)
}
}));
}