Skip to content

supabase.auth.getSession is slow #970

Closed

Description

@lauri865

Bug report

  • [ x ] I confirm this is a bug with Supabase, not with my own application.
  • [ x ] I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

supabase.auth.getSession() can take anywhere from 20ms up to 100ms on a fast processor (M2 Pro) when invoked e.g. during an initial page rendering phase. This feels way too slow for a function that doesn't do any network calls.

For comparison, a custom function I mocked up that reads the cookie, parses accessToken and returns the JWT as an object (json.parse+base64decode+json.parse) takes around 0.3-0.4ms under the same load, and is similarly performant during idle and rendering phase.

Benchmarks (rough):
During rendering phase:

  1. Custom JWT decode: <0.4ms
  2. await supabase.auth.getSession(): ~50ms

Idle phase:

  1. Custom JWT decode: <0.4ms (very stable and predictable)
  2. await supabase.auth.getSession(): 0.8-1.7ms (significantly better, but all over the place even during an idle phase)

Given the above, it's unlikely that it's caused by anything specific to my app, since the custom implementation doesn't slow down at all during rendering phase vs. idle phase.

To Reproduce

  1. Run the below code e.g. when rendering a React component
console.time("getSession");
const { data, error } = await supabase.auth.getSession();
console.timeEnd("getSession");
  1. Refresh the page

Expected behavior

Under 1ms performance for a function that is doing simple local processing..

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • OS: macOS
  • Browser (if applies) Chrome
  • Version of supabase-js: latest
  • Version of Node.js: 20

Additional context

What's worse, it's similarly slow even when there's no cookie present.

Activity

  1. added
    bug
    on Feb 8, 2024
  2. lauri865 commented on Feb 8, 2024

    @lauri865
    ContributorAuthor

    Just checked the implementation - could it be due to the locking mechanism? It's a bit unclear (to an outsider) why the function as a whole needs locks? Refresh session, sure, perhaps not the session fetching part.

    I imagine it's often used for client-side route protections, etc. Which just adds an unnecessary delay to page renders and delay before data fetching starts.

  3. MatthewDlr commented on Apr 27, 2024

    @MatthewDlr

    Just noticed the same thing here!
    Meanwhile this is fixed, can you share your JWT implementation pls ?

    Thx

  4. Ruthenz commented on Jul 9, 2025

    @Ruthenz

    Hey! would love to know if anyone saw this?
    I see for me it also takes 100ms minimum which seems really long for checking a JWT.

  5. lauri865 commented on Aug 18, 2025

    @lauri865
    ContributorAuthor

    We use this code snippet, takes ~0.2ms on my machine, supports chunked cookies as well or any other storage adapter you may use:

    import { Session } from "@supabase/supabase-js";
    
    const getSession = () => {
      const cookie = supabase.auth["storage"].getItem(
        supabase["storageKey"],
      ) as string | null;
    
      if (!cookie) return null;
    
      const session = JSON.parse(cookie) as Partial<Session>;
    
      return session;
    }
  6. github-actions commented on Feb 15, 2026

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has not had any activity for 6 months.
    It will be closed in 30 days if no further activity occurs.

    If this is still relevant, please comment to keep it open.

  7. Vickyfreshmusik commented on Feb 15, 2026

    @Vickyfreshmusik
  8. karan-963 commented on Aug 17, 2026

    @karan-963

    Hey everyone! 👋

    I've opened a PR addressing this: #2618.

    Root Cause & Solution

    During initial page rendering and multi-component mounts (React / Next.js), calling getSession() in parallel caused calls to queue sequentially behind mutual-exclusion locks (_acquireLock) and redundant async storage reads (getItemAsync), compounding latency to 20ms–450ms+.

    The PR introduces:

    1. Lockless Read Path: getSession() no longer blocks in mutual-exclusion lock queues.
    2. In-Flight Single-Flighting: Multiple simultaneous calls during mount share a single in-flight storage load rather than triggering $N$ sequential reads.
    3. In-Memory Cache: Valid, unexpired sessions resolve in <0.001 ms (sub-microsecond), matching raw JWT/cookie parsing floor while retaining caller immutability (deepClone).

    🌐 Live Browser Test Harness

    You can test the fix live in your browser on your own hardware here:
    👉 https://karan-963.github.io/supabase-js/

    Would love for you to test this branch with your setups and share feedback!

  9. mandarini commented on Oct 16, 2026

    @mandarini
    Contributor

    Hi @lauri865, thank you for opening this issue! 💚

    Closing as fixed. The cause was the navigator.locks mutex and queue around getSession(), removed as the default in v2.107.0 (#2392). It is now a storage read plus expiry check, no lock. Passing a custom lock opts back into the old path.

    If you still see this on 2.107.0 or later, open a new issue with the version and timings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions