Skip to content

feat: add cache to defineGetter - #6042

Closed
nigrosimone wants to merge 1 commit into
expressjs:masterfrom
nigrosimone:patch-1
Closed

feat: add cache to defineGetter#6042
nigrosimone wants to merge 1 commit into
expressjs:masterfrom
nigrosimone:patch-1

Conversation

@nigrosimone

@nigrosimonenigrosimone commented Oct 12, 2024

Copy link
Copy Markdown

Adding this cache to the defineGetter function has several implications:

  • Performance Improvement: The getter function will only be called once, and its result will be stored. Subsequent accesses to the property will return the cached value, avoiding the cost of repeatedly executing the getter function.
  • Consistency: The value returned by the getter will remain consistent for the lifetime of the property on the object. If the getter logic involves any computation that could potentially change, this cache will prevent any changes from being reflected in the property's value after the first access.
  • Side Effects: If the getter function has side effects (e.g., logging, triggering other operations), these will only occur once when the value is first computed. Subsequent accesses will not trigger these side effects.

Just for POC, I have enabled only on query

Adding this cache to the defineGetter function has several implications:
Performance Improvement: The getter function will only be called once, and its result will be stored. Subsequent accesses to the property will return the cached value, avoiding the cost of repeatedly executing the getter function.
Consistency: The value returned by the getter will remain consistent for the lifetime of the property on the object. If the getter logic involves any computation that could potentially change, this cache will prevent any changes from being reflected in the property's value after the first access.
Side Effects: If the getter function has side effects (e.g., logging, triggering other operations), these will only occur once when the value is first computed. Subsequent accesses will not trigger these side effects.
@IamLizu

Copy link
Copy Markdown
Member

Referencing expressjs/discussions#306

This was referenced Aug 5, 2026
@krzysdz

Copy link
Copy Markdown
Contributor

Consistency: The value returned by the getter will remain consistent for the lifetime of the property on the object. If the getter logic involves any computation that could potentially change, this cache will prevent any changes from being reflected in the property's value after the first access.

Well, I think one of the reasons why some properties are getters is that they reflect changes to the source object (e.g. req.url).

Caching getters would be a breaking change and definitely cannot land in 5.x.

@krzysdzkrzysdz removed the 5.x label Aug 8, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@nigrosimone@IamLizu@krzysdz@bjohansebas