Skip to content

Performance Issue: AutocompleteInput is slow during typing (proposing useCallback refactor) #1288

@Q00

Description

@Q00

Describe the Bug

The AutocompleteInput component experiences noticeable lag and slowness, especially when the user is typing quickly.

It appears the performance issue is related to the onKeyDown event handler. The handler is currently defined as an inline function, which causes it to be recreated on every single render of the component. This likely triggers unnecessary re-renders in the underlying Mantine Textarea and adds performance overhead during rapid key presses.

Proposed Solution

To fix this, I propose refactoring the event handlers to prevent them from being recreated on every render. This involves using the useCallback hook.

The "delay logic" for fetching suggestions is already correctly handled by useDebounce, so this change will specifically optimize the keyboard event handling itself.

The planned changes are:

  1. Memoize onAcceptSuggestion: Wrap the onAcceptSuggestion helper function in useCallback.
  2. Memoize onKeyDown: Extract the onKeyDown logic into a new function called handleKeyDown and wrap it in useCallback with its proper dependency array (including onAcceptSuggestion, suggestedProperties, value, etc.).
  3. Assign Memoized Handler: Update the Textarea prop to use the memoized handler: <Textarea ... onKeyDown={handleKeyDown} />.

This should significantly improve the responsiveness of the input field by stabilizing the function props passed to the Textarea.

Is this a known issue, or would you be open to a PR with this optimization? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions