forked from mhevery/AngularConnect-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample1.ts
27 lines (22 loc) · 818 Bytes
/
example1.ts
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
const COUNT = Number.parseInt(process.argv[2] || "10");
console.log(`Running ${COUNT} iterations.`);
let value = 0;
function benchA() { value = value === 0 ? 0 : 1; }
function benchB() { value = value === 0 ? 0 : 2; }
function benchC() { value = value === 0 ? 0 : 3; }
function benchD() { value = value === 0 ? 0 : 4; }
//benchmark('-------------------------- IGNORE --------------------------', benchA);
benchmark('A', benchA);
benchmark('B', benchB);
benchmark('C', benchC);
benchmark('D', benchD);
/////////////////////
function benchmark(name: string, fn: ()=> void) {
console.log('Starting:', name, '...');
const start = Date.now();
for(let i=0; i<COUNT; i++) {
fn();
}
const duration = Date.now() - start;
console.log(' ' , name, Number(duration/COUNT*1000*1000).toFixed(3), 'us');
}