From 24fcb0ca1bbc830e6ddb767a74283078ca265a70 Mon Sep 17 00:00:00 2001 From: Inga Date: Mon, 18 Nov 2024 18:55:51 +0000 Subject: [PATCH] minor optimization --- src/shared/board.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/shared/board.ts b/src/shared/board.ts index 76afbf2..1d5f6d7 100644 --- a/src/shared/board.ts +++ b/src/shared/board.ts @@ -3,7 +3,10 @@ import { BoardType, SquareState, formatSquareState, parseSquareState } from "./d export class Board implements BoardType { // State should be immutable - constructor(private readonly state: SquareState[][]) {} + constructor( + private readonly state: SquareState[][], + private serialized?: string, + ) {} hasRow(row: number) { return !!this.state[row]; @@ -53,10 +56,11 @@ export class Board implements BoardType { return squareState; }), ), + serialized, ); } - serialize() { + private forceSerialize() { return this.state .map((row) => row @@ -72,4 +76,13 @@ export class Board implements BoardType { ) .join("|"); } + + serialize() { + if (!this.serialized) { + const serialized = this.forceSerialize(); + this.serialized = serialized; + } + + return this.serialized; + } }