-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbenchmark.js
More file actions
48 lines (45 loc) · 1.23 KB
/
benchmark.js
File metadata and controls
48 lines (45 loc) · 1.23 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Naive implementation
* @param {string} input Input string
*/
function capitalizeFirst(input) {
let output = '';
for (let i = 0; i < input.length; i++) {
if (i === 0) {
output += input[0].toUpperCase();
} else {
output += input[i];
}
}
return output;
}
/**
* Improved implementation
* @param {string} input Input string
*/
function capitalizeFirstRefactored(input) {
return input.length > 0
? input[0].toUpperCase() + input.slice(1)
: '';
}
/**
* Benchmarking function
* @param {*} label Label for the benchmark
* @param {*} fn Function to benchmark
* @param {...any} rest Arguments passed to the function
*/
const benchmark = (label, fn, ...rest) => {
// Record timestamp before running benchmark
const dateStart = new Date();
// Run the function 1M times
for (let i = 0; i < 1000000; i++) {
fn(...rest);
}
// Record timestamp after running benchmark
const dateEnd = new Date();
// Compute milliseconds diff between end and start timestamps
const diff = dateEnd.getTime() - dateStart.getTime();
console.log(label, diff + 'ms');
}
benchmark('naive implementation', capitalizeFirst, 'javaScript');
benchmark('improved implementation', capitalizeFirstRefactored, 'javaScript');