Skip to content

Commit

Permalink
add a benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Aug 28, 2018
1 parent b35690c commit c5e2d08
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
65 changes: 65 additions & 0 deletions bench.mjs
Original file line number Diff line number Diff line change
@@ -0,0 1,65 @@

import ShelfPack from 'shelf-pack';
import binpack from 'bin-pack';
import potpack from './index.mjs';

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`);
}
14 changes: 5 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 11,15 @@
"index.js"
],
"scripts": {
"pretest": "eslint index.mjs",
"pretest": "eslint index.mjs test.mjs bench.mjs",
"test": "tape -r esm test.js",
"bench": "node -r esm bench.mjs",
"build": "rollup -c",
"start": "rollup -w",
"prepublishOnly": "npm run build"
},
"eslintConfig": {
"extends": "mourner",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-var": "error",
"prefer-const": "error"
}
"extends": "mourner"
},
"repository": {
"type": "git",
Expand All @@ -45,11 39,13 @@
},
"homepage": "https://mapbox.github.io/potpack/",
"devDependencies": {
"bin-pack": "^1.0.2",
"eslint": "^5.4.0",
"eslint-config-mourner": "^3.0.0",
"esm": "^3.0.80",
"rollup": "^0.65.0",
"rollup-plugin-buble": "^0.19.2",
"shelf-pack": "^2.0.1",
"tape": "^4.9.1"
}
}

0 comments on commit c5e2d08

Please sign in to comment.