Skip to content

perf(index): remove redundant optional chaining#296

Merged
Fdawgs merged 1 commit intomainfrom
fix/optional-chaining
Nov 29, 2025
Merged

perf(index): remove redundant optional chaining#296
Fdawgs merged 1 commit intomainfrom
fix/optional-chaining

Conversation

@Fdawgs
Copy link
Member

@Fdawgs Fdawgs commented Nov 29, 2025

routeOptions.config is always defined, so the optional chaining is redundant. Redundant optional chaining has a small performance impact: https://adventures.nodeland.dev/archive/noop-functions-vs-optional-chaining-a-performance/

For example, a basic route of:

fastify.get('/', async () => {
    return { message: 'ok' }
  })

would have the following routeOptions property:

{
  method: "GET",
  url: "/",
  bodyLimit: 1048576,
  attachValidation: false,
  logLevel: "",
  exposeHeadRoute: true,
  prefixTrailingSlash: "both",
  handler: function () { [native code] },
  config: {
    url: "/",
    method: "GET",
  },
  schema: undefined,
  version: undefined,
}

Checklist

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a potential TypeError by adding a safe fallback when destructuring request.routeOptions?.config. The optional chaining operator ?. can short-circuit to undefined when either request.routeOptions or config is undefined, and destructuring from undefined throws a TypeError.

Key Changes:

  • Added ?? {} fallback for optional chaining destructuring in two onRequest hooks

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

@Fdawgs Fdawgs changed the title fix(index): add fallback for optional chaining destruct refactor(index): remove redundant optional chaining Nov 29, 2025
@Fdawgs Fdawgs force-pushed the fix/optional-chaining branch from 7bd4a67 to 2ac1499 Compare November 29, 2025 10:41
@Fdawgs Fdawgs requested a review from Copilot November 29, 2025 10:58
@Fdawgs Fdawgs changed the title refactor(index): remove redundant optional chaining perf(index): remove redundant optional chaining Nov 29, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

@Fdawgs Fdawgs merged commit b98e980 into main Nov 29, 2025
23 checks passed
@Fdawgs Fdawgs deleted the fix/optional-chaining branch November 29, 2025 11:02
Sign up for free to 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.

3 participants