-
Notifications
You must be signed in to change notification settings - Fork 30
/
benchmark.js
124 lines (103 loc) · 2.49 KB
/
benchmark.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const CREATE = 50000;
const ECS = require('./src/index');
const perf_hooks = require('perf_hooks');
const descriptions = {
create2Comp: 'Create 50,000 entities with two simple components ',
destroy2Comp: 'Destroy 50,000 entities with two simple components',
recreating: 'Recreating components now that pool is established',
rewriteComp: 'Changing the values of each component '
}
const times = {
create2Comp: 0,
destroy2Comp: 0,
recreating: 0
};
function output(test) {
console.log(`${descriptions[test]}: ${(times[test]).toFixed(2)}ms`);
}
class Test extends ECS.Component {
static properties = {
a: 1,
b: 2
};
}
class Test2 extends ECS.Component {
static properties = {
c: 3,
d: 4
};
}
function benchmarks() {
let start, end;
const ecs = new ECS.World({ trackChanges: false, entityPool: 100 });
ecs.registerComponent(Test);
ecs.registerComponent(Test2);
const entities = [];
console.log(`Creating and destroying ${CREATE} entities...`);
start = perf_hooks.performance.now();
for (let i = 0; i < CREATE; i++) {
entities.push(
ecs.createEntity({
components: [
{
type: 'Test',
key: 'Test',
a: 4,
b: 5
},
{
type: 'Test2',
key: 'Test2',
c: 6,
d: 7
}
]
})
);
}
end = perf_hooks.performance.now();
times.create2Comp = end - start;
output('create2Comp');
start = perf_hooks.performance.now();
for (let i = 0; i < CREATE; i++) {
entities[i].c.Test.a = 14;
entities[i].c.Test.b = 15;
entities[i].c.Test2.c = 16;
entities[i].c.Test2.d = 17;
}
end = perf_hooks.performance.now();
times.rewriteComp = end - start;
output('rewriteComp');
start = perf_hooks.performance.now();
for (let i = 0; i < CREATE; i++) {
entities[i].destroy();
}
end = perf_hooks.performance.now();
times.destroy2Comp = end - start;
output('destroy2Comp');
start = perf_hooks.performance.now();
for (let i = 0; i < CREATE; i++) {
entities.push(
ecs.createEntity({
components: [
{
type: 'Test',
lookup: 'Test',
a: 4,
b: 5
},
{
type: 'Test2',
lookup: 'Test2',
c: 6,
d: 7
}
]
})
);
}
end = perf_hooks.performance.now();
times.recreating = end - start;
output('recreating');
}
benchmarks();