REFACTOR: Make getPlayer 10x faster (#2548)

This commit is contained in:
David Walker
2026-03-03 14:21:34 -08:00
committed by GitHub
parent 5e71612fd7
commit dc5c43db2e
4 changed files with 54 additions and 5 deletions

11
package-lock.json generated
View File

@@ -96,6 +96,7 @@
"prettier": "^2.8.8",
"react-refresh": "^0.18.0",
"style-loader": "^4.0.0",
"tinybench": "^6.0.0",
"typescript": "^5.9.3",
"webpack": "^5.100.2",
"webpack-cli": "^6.0.1",
@@ -17780,6 +17781,16 @@
"resolved": "https://registry.npmjs.org/tiny-warning/-/tiny-warning-1.0.3.tgz",
"integrity": "sha512-lBN9zLN/oAf68o3zNXYrdCt1kP8WsiGW8Oo2ka41b2IM5JL/S1CTyX1rW0mb/zSuJun0ZUrDxx4sqvYS2FWzPA=="
},
"node_modules/tinybench": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/tinybench/-/tinybench-6.0.0.tgz",
"integrity": "sha512-BWlWpVbbZXaYjRV0twGLNQO00Zj4HA/sjLOQP2IvzQqGwRGp+2kh7UU3ijyJ3ywFRogYDRbiHDMrUOfaMnN56g==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=20.0.0"
}
},
"node_modules/tldts": {
"version": "6.1.86",
"resolved": "https://registry.npmjs.org/tldts/-/tldts-6.1.86.tgz",

View File

@@ -97,6 +97,7 @@
"prettier": "^2.8.8",
"react-refresh": "^0.18.0",
"style-loader": "^4.0.0",
"tinybench": "^6.0.0",
"typescript": "^5.9.3",
"webpack": "^5.100.2",
"webpack-cli": "^6.0.1",

View File

@@ -1422,17 +1422,17 @@ export const ns: InternalAPI<NSFull> = {
getPlayer: () => () => {
const data = {
// Person
hp: structuredClone(Player.hp),
skills: structuredClone(Player.skills),
exp: structuredClone(Player.exp),
mults: structuredClone(Player.mults),
hp: { ...Player.hp },
skills: { ...Player.skills },
exp: { ...Player.exp },
mults: { ...Player.mults },
city: Player.city,
// Player-specific
numPeopleKilled: Player.numPeopleKilled,
money: Player.money,
location: Player.location,
totalPlaytime: Player.totalPlaytime,
jobs: structuredClone(Player.jobs),
jobs: { ...Player.jobs },
factions: Player.factions.slice(),
entropy: Player.entropy,
karma: Player.karma,

View File

@@ -0,0 +1,37 @@
import { Bench } from "tinybench";
import { initGameEnvironment, setupBasicTestingEnvironment, getWorkerScriptAndNS } from "../Utilities";
import { Player } from "@player";
initGameEnvironment();
beforeEach(() => {
setupBasicTestingEnvironment();
});
test("getPlayer", () => {
const { ns } = getWorkerScriptAndNS();
const props = [
"hp",
"skills",
"exp",
"mults",
"city",
"numPeopleKilled",
"money",
"location",
"totalPlaytime",
"jobs",
"factions",
"entropy",
"karma",
] as const;
const expected = Object.fromEntries(props.map((k) => [k, Player[k]]));
expect(ns.getPlayer()).toEqual(expected);
});
// Benchmarks shouldn't waste time on every test run. Un-skip this when evaluating changes.
test.skip("Benchmark getPlayer", async () => {
const { ns } = getWorkerScriptAndNS();
const bench = new Bench().add("getPlayer", () => ns.getPlayer());
await bench.run();
console.table(bench.table());
});