-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathperftest.js
74 lines (61 loc) · 1.55 KB
/
perftest.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Big has 1000 members in its prototype
function Big() {}
let symBig;
for(let i = 0; i < 1000; i++) {
const s = Symbol('small')
if (i === 500)
symBig = s;
Big.prototype[s] = function() {return 0;}
}
// Small just has one member in its prototype
function Small() {}
const symSmall = Symbol('big');
Small.prototype[symSmall] = function() {return 0;};
// Access a member function 1M times (also some math overhead and looping)
const NO_TIMES = 10000000;
function testSmallInner(obj) {
let a = 0;
for (let i = 0; i < NO_TIMES; i++) {
a = a + obj[symSmall]();
}
}
function testSmall(obj) {
console.time('Small');
testSmallInner(obj);
console.timeEnd('Small');
}
function testBigInner(obj) {
let a = 0;
for (let i = 0; i < NO_TIMES; i++) {
a = a + obj[symBig]();
}
}
function testBig(obj) {
console.time('Big');
testBigInner(obj);
console.timeEnd('Big');
}
// testSmall();
// testBig();
// Wait for any code optimization to finish
// setTimeout(() => {
testSmallInner(new Small());
testBigInner(new Big());
testSmallInner(new Small());
testBigInner(new Big());
const big = new Big();
const small = new Small();
console.log('TurboFan:');
testSmall(small);
testBig(big);
// see https://blog.logrocket.com/how-javascript-works-optimizing-the-v8-compiler-for-efficiency/
const TURBO_FAN_LIMIT = 4;
for (let i = 0; i < TURBO_FAN_LIMIT; i++) {
big['x' + i] = 0;
small['x' + i] = 0;
testSmallInner(small);
testBigInner(big);
}
console.log('\nIgnition:');
testSmall(small);
testBig(big);