-
Notifications
You must be signed in to change notification settings - Fork 56
/
bench.js
126 lines (108 loc) · 3.19 KB
/
bench.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
123
124
125
126
import Flatbush from './index.js';
import RBush from 'rbush';
import rbushKNN from 'rbush-knn';
const N = 1000000;
const K = 1000;
const nodeSize = 16;
console.log(`${N} rectangles`);
console.log(`node size: ${nodeSize}`);
console.log('');
function addRandomBox(arr, boxSize) {
const x = Math.random() * (100 - boxSize);
const y = Math.random() * (100 - boxSize);
const x2 = x + Math.random() * boxSize;
const y2 = y + Math.random() * boxSize;
arr.push(x, y, x2, y2);
}
const coords = [];
for (let i = 0; i < N; i++) addRandomBox(coords, 1);
const boxes100 = [];
const boxes10 = [];
const boxes1 = [];
for (let i = 0; i < K; i++) {
addRandomBox(boxes100, 100 * Math.sqrt(0.1));
addRandomBox(boxes10, 10);
addRandomBox(boxes1, 1);
}
console.time('flatbush');
const index = new Flatbush(N, nodeSize);
for (let i = 0; i < coords.length; i += 4) {
index.add(
coords[i],
coords[i + 1],
coords[i + 2],
coords[i + 3]);
}
index.finish();
console.timeEnd('flatbush');
console.log(`index size: ${index.data.byteLength.toLocaleString()}`);
function benchSearch(boxes, name, warmup) {
const id = `${K} searches ${name}`;
if (!warmup) console.time(id);
for (let i = 0; i < boxes.length; i += 4) {
index.search(boxes[i], boxes[i + 1], boxes[i + 2], boxes[i + 3]);
}
if (!warmup) console.timeEnd(id);
}
function benchNeighbors(K, M, warmup) {
const id = `${K} searches of ${M} neighbors`;
if (!warmup) console.time(id);
for (let i = 0; i < K; i++) {
index.neighbors(coords[4 * i], coords[4 * i + 1], M);
}
if (!warmup) console.timeEnd(id);
}
benchSearch(boxes1, '0.01%', true);
benchSearch(boxes100, '10%');
benchSearch(boxes10, '1%');
benchSearch(boxes1, '0.01%');
benchNeighbors(K, 1, true);
benchNeighbors(K, 100);
benchNeighbors(1, N);
benchNeighbors(N / 10, 1);
const dataForRbush = [];
for (let i = 0; i < coords.length; i += 4) {
dataForRbush.push({
minX: coords[i],
minY: coords[i + 1],
maxX: coords[i + 2],
maxY: coords[i + 3]
});
}
console.log('');
console.time('rbush');
const rbushIndex = new RBush(nodeSize).load(dataForRbush);
console.timeEnd('rbush');
function benchSearchRBush(boxes, name, warmup) {
const boxes2 = [];
for (let i = 0; i < boxes.length; i += 4) {
boxes2.push({
minX: boxes[i],
minY: boxes[i + 1],
maxX: boxes[i + 2],
maxY: boxes[i + 3]
});
}
const id = `${K} searches ${name}`;
if (!warmup) console.time(id);
for (let i = 0; i < boxes2.length; i++) {
rbushIndex.search(boxes2[i]);
}
if (!warmup) console.timeEnd(id);
}
function benchNeighborsRBush(K, M, warmup) {
const id = `${K} searches of ${M} neighbors`;
if (!warmup) console.time(id);
for (let i = 0; i < K; i++) {
rbushKNN(rbushIndex, coords[4 * i], coords[4 * i + 1], M);
}
if (!warmup) console.timeEnd(id);
}
benchSearchRBush(boxes1, '0.01%', true);
benchSearchRBush(boxes100, '10%');
benchSearchRBush(boxes10, '1%');
benchSearchRBush(boxes1, '0.01%');
benchNeighborsRBush(K, 1, true);
benchNeighborsRBush(K, 100);
benchNeighborsRBush(1, N);
benchNeighborsRBush(N / 10, 1);