Skip to content

perf: getContactById calls fetchBizProfile on every call instead of using cached collection #201656

@Adi1231234

Description

@Adi1231234

Issue Priority

Medium

Issue Description

getContactById (and msg.getContact()) calls Store.BusinessProfile.fetchBizProfile(wid) on every invocation. This function always makes a network round-trip to WhatsApp servers (~85-100ms per call), even when the result is already available in the local BusinessProfileCollection.

The BusinessProfileCollection already has find() and get() methods that check the local cache first and only fetch from the server when the entry is missing, exactly like Store.Contact.find() works for contacts.

Since getContact is often called multiple times per message (from event handlers, middleware, etc.), this adds hundreds of milliseconds of unnecessary latency per message. Under load (e.g. receiving many messages at once), the concurrent network calls can stack up significantly.

Steps To Reproduce

  1. Connect a client and receive a message
  2. Call msg.getContact() - takes ~100ms due to fetchBizProfile
  3. Call msg.getContact() again for the same contact - still ~100ms
  4. Meanwhile, Store.BusinessProfile.get(wid) returns the same result in 0ms

Evidence

Tested directly in WhatsApp Web DevTools console:

// fetchBizProfile - always makes a network call
Store.BusinessProfile.fetchBizProfile(wid)  ->  ~95ms (every time)

// get() - reads from local collection cache
Store.BusinessProfile.get(wid)              ->  0.005ms

// find() - cache first, fetches only if missing
Store.BusinessProfile.find(wid)             ->  0.065ms (cached) / ~85ms (uncached)

All three methods return the exact same object reference (=== is true) when the entry exists in the cache.

For LID contacts, the first fetchBizProfile call can take 500-750ms, making the impact even larger.

Proposed Fix

In src/util/Injected/Utils.js, replace:

const bizProfile = await window.Store.BusinessProfile.fetchBizProfile(wid);

with:

const bizProfile = await window.Store.BusinessProfile.find(wid);

find() checks the local BusinessProfileCollection cache first and only fetches from the server when the entry is not present. This matches the pattern already used for Store.Contact.find(wid) on the line above.

WhatsApp Account Type

Standard

Authentication Strategy

LocalAuth

whatsapp-web.js Version

1.26.1-alpha.2

WhatsApp Web Version

2.3000+

Browser Type

Chromium

Browser Version

Chrome 145

Computer OS Version

Windows 11

Phone OS Version

Android 14

Node.js Version

20.x

Checklist

  • I'm using the latest version of whatsapp-web.js
  • I've searched existing issues for similar reports
  • I've verified this is a library issue, not a WhatsApp limitation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions