Skip to content

Instantly share code, notes, and snippets.

@endel
Last active October 25, 2017 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endel/72e4a3e219e2ff4ed21240d50621668b to your computer and use it in GitHub Desktop.
Save endel/72e4a3e219e2ff4ed21240d50621668b to your computer and use it in GitHub Desktop.
import { toJSON } from "../../src/Utils";
const suite = new Benchmark.Suite();
const NUM_CLIENTS = 2;
const NUM_MONSTERS = 500;
const MAP_GRID_SIZE = 200;
// build 200x200 map
const map = [];
for (var i=0; i<MAP_GRID_SIZE; i++) {
map[i] = [];
for (var j=0; j<MAP_GRID_SIZE; j++) {
map[i].push( (Math.random() > 0.5) ? 0 : 1 );
}
}
// build 500 monsters
let monsters = {};
for (let i=0; i<NUM_MONSTERS; i++) {
monsters[ generateId() ] = {
x: Math.round(Math.random() * 200),
y: Math.round(Math.random() * 200)
};
}
// build 10 players
let players = {};
for (let i=0; i<NUM_CLIENTS; i++) {
players[ generateId() ] = {
x: Math.round(Math.random() * 200),
y: Math.round(Math.random() * 200)
};
}
function moveMonsters () {
for (let id in monsters) {
monsters[ id ].x += (Math.random() > 0.5) ? 1 : -1;
monsters[ id ].y += (Math.random() > 0.5) ? 1 : -1;
}
}
function movePlayers () {
for (let id in players) {
players[ id ].x += (Math.random() > 0.5) ? 1 : -1;
players[ id ].y += (Math.random() > 0.5) ? 1 : -1;
}
}
let state = { map, monsters, players };
suite.add('toJSON', () => toJSON(state));
suite.on('cycle', e => console.log( e.target.toString() ));
suite.run();
console.log("Done.")
process.exit();
toJSON x 13.63 ops/sec ±6.76% (35 runs sampled)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment