-
Notifications
You must be signed in to change notification settings - Fork 22
/
bench.js
65 lines (56 loc) · 1.6 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
import ShelfPack from '@mapbox/shelf-pack';
import binpack from 'bin-pack';
import potpack from './index.js';
const N = 10000;
const randBox = () => {
const sizes = [12, 16, 20, 24];
return {
w: sizes[Math.floor(Math.random() * sizes.length)],
h: sizes[Math.floor(Math.random() * sizes.length)],
x: 0,
y: 0
};
};
const randBox2 = () => ({
w: 1 + Math.floor(Math.random() * 99),
h: 1 + Math.floor(Math.random() * 99),
x: 0,
y: 0
});
const randBoxBinPack = (genBox) => {
const {w, h} = genBox();
return () => ({width: w, height: h, x: 0, y: 0});
};
const shelfPackIter = (boxes) => {
const pack = new ShelfPack(64, 64, {autoResize: true});
pack.pack(boxes, {inPlace: true});
};
const potpackIter = (boxes) => {
potpack(boxes);
};
const binpackIter = (boxes) => {
binpack(boxes);
};
console.log('4 different sizes:');
bench(' bin-pack', binpackIter, randBoxBinPack(randBox));
bench(' shelf-pack', shelfPackIter, randBox);
bench(' potpack', potpackIter, randBox);
console.log('\n100 different sizes:');
bench(' bin-pack', binpackIter, randBoxBinPack(randBox2));
bench(' shelf-pack', shelfPackIter, randBox2);
bench(' potpack', potpackIter, randBox2);
function bench(id, fn, genBox) {
const boxes = [];
for (let i = 0; i < N; i++) {
boxes.push(genBox());
}
const start = process.hrtime();
let s, ns;
let k = 0;
do {
fn(boxes.slice());
[s, ns] = process.hrtime(start);
k++;
} while (s < 1);
console.log(`${id}: ${Math.round(100 * k / (s + ns / 1e9)) / 100} ops/s`);
}