Skip to content

Commit 221b0d5

Browse files
committed
add benchmark
1 parent 7fc8979 commit 221b0d5

3 files changed

Lines changed: 58 additions & 1 deletion

File tree

benchmark/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2019 The boardgame.io Authors
3+
*
4+
* Use of this source code is governed by a MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
import Benchmark from 'benchmark';
10+
import Game from '../src/core/game';
11+
import { InitializeGame, CreateGameReducer } from '../src/core/reducer';
12+
import { makeMove, gameEvent } from '../src/core/action-creators';
13+
14+
const game = Game({
15+
moves: {
16+
A: G => G,
17+
},
18+
flow: {
19+
endGameIf: (G, ctx) => (G.victory ? ctx.currentPlayer : undefined),
20+
},
21+
});
22+
23+
const reducer = CreateGameReducer({ game });
24+
const state = InitializeGame({ game });
25+
26+
new Benchmark.Suite()
27+
.add('move', function() {
28+
reducer(state, makeMove('A'));
29+
})
30+
.add('endTurn', function() {
31+
reducer(state, gameEvent('endTurn'));
32+
})
33+
.on('cycle', function(event) {
34+
console.log(String(event.target));
35+
})
36+
.on('complete', function() {
37+
console.log('Fastest is ' + this.filter('fastest').map('name'));
38+
})
39+
.run({ async: true });

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"dev:client": "(cd examples/react-web; npm start)",
99
"dev:server": "cross-env NODE_ENV=development nodemon -w src -w examples -e js,ts --exec babel-node --extensions '.ts,.js' --ignore 'src/**/*.test.ts' --presets @babel/preset-env examples/react-web/server.js",
1010
"build": "cross-env BABEL_ENV=rollup rollup --config rollup.npm.js",
11+
"benchmark": "babel-node --presets @babel/preset-env benchmark/index.js",
1112
"docsify": "docsify serve docs",
1213
"docsify:update": "npm run build && node ./scripts/docsify-update.js",
1314
"examples": "npm run start",
@@ -21,7 +22,7 @@
2122
"lint:fix": "eslint --fix .",
2223
"prepack": "npm run build && generate-export-aliases",
2324
"postpack": "npm run clean",
24-
"prettier": "prettier --write \"{examples,src}/**/*.{js,css,md}\"",
25+
"prettier": "prettier --write \"{examples,src,benchmark}/**/*.{js,css,md}\"",
2526
"changelog": "bash ./scripts/changelog.sh",
2627
"clean": "node ./scripts/clean.js"
2728
},
@@ -81,6 +82,7 @@
8182
"babel-eslint": "^8.0.0",
8283
"babel-loader": "^8.0.5",
8384
"babel-plugin-module-resolver": "^3.0.0",
85+
"benchmark": "^2.1.4",
8486
"concurrently": "^4.1.0",
8587
"coveralls": "^3.0.0",
8688
"cross-env": "^5.1.1",

0 commit comments

Comments
 (0)