-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.ts
83 lines (69 loc) · 1.89 KB
/
benchmark.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
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
import { BoundlessGrid } from "./src"
import { table } from "table"
const now = performance.now
type Entity = {
id: number
position: {
x: number
y: number
z: number
}
}
const makeEntities = (num: number): Entity[] => {
const entities = []
for (let i = 0; i < num; i++) {
entities.push({
id: i,
position: {
x: Math.random() * 1000 - 1000,
y: Math.random() * 1000 - 1000,
z: Math.random() * 1000 - 1000
}
})
}
return entities
}
const addEntities = (iterations = 100, numEntities = 10_000) => {
/* Create entities */
const entities = makeEntities(numEntities)
const grid = new BoundlessGrid(50)
const start = now()
for (let i = 0; i < iterations; i++) {
/* Add objects to grid */
for (const entity of entities) {
grid.placeEntity(entity, entity.position)
}
grid.clear()
}
const stop = now()
const time = (stop - start) / iterations
return [`Adding ${numEntities} entities:`, time.toFixed(3)]
}
const moveEntities = (iterations = 100, numEntities = 10_000) => {
/* Create entities */
const entities = makeEntities(numEntities)
const grid = new BoundlessGrid(50)
for (const entity of entities) {
grid.placeEntity(entity, entity.position)
}
const start = now()
for (let i = 0; i < iterations; i++) {
/* Move and update all entities */
for (const entity of entities) {
entity.position.x = Math.random() * 1000 - 1000
entity.position.y = Math.random() * 1000 - 1000
entity.position.z = Math.random() * 1000 - 1000
grid.placeEntity(entity, entity.position)
}
}
const stop = now()
const time = (stop - start) / iterations
return [`Moving ${numEntities} entities:`, time.toFixed(3)]
}
const results = [
["Benchmark Name", "Average Time per Iteration (ms)"],
addEntities(),
moveEntities()
]
console.log("\n⏰ RESULTS\n")
console.log(table(results))