Skip to content

Conversation

@purpshell
Copy link
Member

This is an experimental change, so I thought I will put it in a PR.

This PR does one thing: it caches each node the getBinaryNodeChild and getBinaryNodeChildren function receives, so we don't have to traverse the entire children array to know where each node is.

This implementation uses the WeakMap , which means all the nodes will be GC'd automatically when no longer in use. Memory leaks remain a point of discussion for this PR.

This PR achieves 30x speed in getBinaryNodeChildren and 10x speed in getBinaryNodeChild.

@whiskeysockets-bot
Copy link
Contributor

whiskeysockets-bot commented Nov 20, 2025

Thanks for opening this pull request and contributing to the project!

The next step is for the maintainers to review your changes. If everything looks good, it will be approved and merged into the main branch.

In the meantime, anyone in the community is encouraged to test this pull request and provide feedback.

✅ How to confirm it works

If you’ve tested this PR, please comment below with:

Tested and working ✅

This helps us speed up the review and merge process.

📦 To test this PR locally:

# NPM
npm install @whiskeysockets/baileys@WhiskeySockets/Baileys#feat/cached-getBinary

# Yarn (v2+)
yarn add @whiskeysockets/baileys@WhiskeySockets/Baileys#feat/cached-getBinary

# PNPM
pnpm add @whiskeysockets/baileys@WhiskeySockets/Baileys#feat/cached-getBinary

If you encounter any issues or have feedback, feel free to comment as well.

@purpshell purpshell force-pushed the feat/cached-getBinary branch from 476e4e9 to 84f159f Compare November 20, 2025 10:05
@purpshell purpshell force-pushed the feat/cached-getBinary branch from bd27831 to e2be5c9 Compare November 20, 2025 10:16
Salientekill pushed a commit to Salientekill/Baileys that referenced this pull request Nov 20, 2025
…aster processing

Implements node caching using WeakMap to dramatically improve performance
when accessing binary node children.

Performance improvements:
- 30x faster getBinaryNodeChildren operations
- 10x faster getBinaryNodeChild operations

Implementation:
- Uses WeakMap for automatic garbage collection
- No memory leaks - nodes are GC'd when no longer in use
- Caches node locations to avoid traversing entire children array

This is an experimental performance optimization that significantly speeds up
binary node processing operations throughout the codebase.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@purpshell purpshell force-pushed the feat/cached-getBinary branch from 6c1bf68 to e2be5c9 Compare November 21, 2025 15:10
Salientekill pushed a commit to Salientekill/Baileys that referenced this pull request Nov 24, 2025
Salientekill pushed a commit to Salientekill/Baileys that referenced this pull request Nov 24, 2025
Salientekill pushed a commit to Salientekill/Baileys that referenced this pull request Nov 24, 2025
Salientekill pushed a commit to Salientekill/Baileys that referenced this pull request Nov 25, 2025
Salientekill pushed a commit to Salientekill/Baileys that referenced this pull request Nov 25, 2025
Salientekill pushed a commit to Salientekill/Baileys that referenced this pull request Nov 25, 2025
Salientekill pushed a commit to Salientekill/Baileys that referenced this pull request Nov 27, 2025
…rformance

Add WeakMap-based caching for binary node child lookups to improve performance.

Changes:
- Add indexCache using WeakMap to cache node child indexes
- Optimize getBinaryNodeChildren to use cached index
- Build index once per node and reuse for subsequent lookups
- Update getBinaryNodeChild to use new optimized getBinaryNodeChildren
- Increase type safety with proper return types

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@purpshell purpshell changed the title Cache the nodes we are processing to access them quickly Cache the children after a getBinaryNodeChild/ren call to avoid traversing arrays Dec 5, 2025
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.

4 participants