From 6d7e7b07ca78907fe69ec48f0a0f255084230059 Mon Sep 17 00:00:00 2001 From: Alexandre Stahmer <47224540+astahmer@users.noreply.github.com> Date: Mon, 22 Jan 2024 14:45:24 +0100 Subject: [PATCH] perf: cache computed properties with loops (#2053) --- .changeset/two-plants-jump.md | 5 +++++ packages/core/src/breakpoints.ts | 32 +++++++++++++++++--------------- packages/core/src/patterns.ts | 6 ++---- packages/core/src/recipes.ts | 5 ++--- 4 files changed, 26 insertions(+), 22 deletions(-) create mode 100644 .changeset/two-plants-jump.md diff --git a/.changeset/two-plants-jump.md b/.changeset/two-plants-jump.md new file mode 100644 index 000000000..e646cfd03 --- /dev/null +++ b/.changeset/two-plants-jump.md @@ -0,0 +1,5 @@ +--- +'@pandacss/core': patch +--- + +Slight perf improvement by caching a few computed properties that contains a loop diff --git a/packages/core/src/breakpoints.ts b/packages/core/src/breakpoints.ts index 66c02b880..bf3822fcb 100644 --- a/packages/core/src/breakpoints.ts +++ b/packages/core/src/breakpoints.ts @@ -3,18 +3,18 @@ import type { RawCondition } from '@pandacss/types' import type { Root } from 'postcss' export class Breakpoints { - constructor(private breakpoints: Record) {} - - get sorted() { - return sortBreakpoints(this.breakpoints) - } - - get values() { - return Object.fromEntries(this.sorted) - } - - get keys() { - return ['base', ...Object.keys(this.values)] + sorted: ReturnType + values: Record + keys: string[] + ranges: Record + conditions: Record + + constructor(private breakpoints: Record) { + this.sorted = sortBreakpoints(breakpoints) + this.values = Object.fromEntries(this.sorted) + this.keys = ['base', ...Object.keys(this.values)] + this.ranges = this.getRanges() + this.conditions = this.getConditions() } get = (name: string) => { @@ -31,7 +31,7 @@ export class Breakpoints { return this.build({ min, max }) } - get ranges(): Record { + private getRanges = () => { const breakpoints: string[] = Object.keys(this.values) const permuations = getPermutations(breakpoints) @@ -57,8 +57,9 @@ export class Breakpoints { return Object.fromEntries(values) } - get conditions(): Record { + private getConditions = () => { const values = Object.entries(this.ranges).map(([key, value]) => { + // console.log(key, value) return [key, toCondition(key, value)] }) @@ -81,7 +82,8 @@ export class Breakpoints { } } -type Entries = [string, { name: string; min?: string | null; max?: string | null }][] +type BreakpointEntry = { name: string; min?: string | null; max?: string | null } +type Entries = [string, BreakpointEntry][] function adjust(value: string | null | undefined) { const computedMax = parseFloat(toPx(value!) ?? '') - 0.04 diff --git a/packages/core/src/patterns.ts b/packages/core/src/patterns.ts index 46752ad09..c3bcd4afc 100644 --- a/packages/core/src/patterns.ts +++ b/packages/core/src/patterns.ts @@ -14,12 +14,14 @@ interface PatternOptions { export class Patterns { patterns: Record details: PatternNode[] + keys: string[] private utility: Utility private tokens: TokenDictionary constructor(options: PatternOptions) { this.patterns = options.config.patterns ?? {} this.details = Object.entries(this.patterns).map(([name, pattern]) => this.createDetail(name, pattern)) + this.keys = Object.keys(this.patterns) this.utility = options.utility this.tokens = options.tokens } @@ -39,10 +41,6 @@ export class Patterns { } } - get keys(): string[] { - return Object.keys(this.patterns) - } - getConfig(name: string): PatternConfig { return this.patterns[name] } diff --git a/packages/core/src/recipes.ts b/packages/core/src/recipes.ts index 1e81ec97d..4604f09a6 100644 --- a/packages/core/src/recipes.ts +++ b/packages/core/src/recipes.ts @@ -38,9 +38,7 @@ const sharedState = { export class Recipes { slotSeparator = '__' - get keys() { - return Object.keys(this.recipes) - } + keys: string[] = [] private context!: SerializeContext @@ -76,6 +74,7 @@ export class Recipes { for (const [name, recipe] of Object.entries(this.recipes)) { this.saveOne(name, recipe) } + this.keys = Object.keys(this.recipes) } saveOne = (name: string, recipe: RecipeConfig | SlotRecipeConfig) => {