diff --git a/package-lock.json b/package-lock.json index 2212ab945..c911ed8ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 68f29d273..ed11e5916 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 19bc82d5c..99924deb1 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -1422,17 +1422,17 @@ export const ns: InternalAPI = { 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, diff --git a/test/jest/Netscript/GetPlayer.test.ts b/test/jest/Netscript/GetPlayer.test.ts new file mode 100644 index 000000000..a04e640db --- /dev/null +++ b/test/jest/Netscript/GetPlayer.test.ts @@ -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()); +});