Skip to content

Conversation

@Praveenkroot
Copy link

@Praveenkroot Praveenkroot commented Jul 25, 2024

Add debounce function to improve performance of @mentions remote filter

  • Integrated debounce function to throttle the frequency of remoteFilter calls.
  • Enhanced user experience by reducing the number of AJAX requests made during typing.
  • Updated the remoteFilter callback in mentions.js to use debounce.
  • Adjusted debounce delay to 300ms to balance responsiveness and performance.

This change aims to optimize the performance of the @mentions feature by minimizing unnecessary AJAX requests and improving overall responsiveness.

Trac ticket: https://buddypress.trac.wordpress.org/ticket/9239


This Pull Request is for code review only. Please keep all other discussion in the BuddyPress Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the WordPress Core Handbook for more details.

Add debounce function to improve performance of @mentions remote filter

- Integrated debounce function to throttle the frequency of remoteFilter calls.
- Enhanced user experience by reducing the number of AJAX requests made during typing.
- Updated the remoteFilter callback in mentions.js to use debounce.
- Adjusted debounce delay to 300ms to balance responsiveness and performance.

This change aims to optimize the performance of the @mentions feature by minimizing unnecessary AJAX requests and improving overall responsiveness.
* @param {number} wait The number of milliseconds to wait before calling `func`.
* @returns {Function} A debounced version of the provided function.
*/
function debounce(func, wait) {
Copy link
Member

Choose a reason for hiding this comment

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

We actually have some prior art. Could we add this in a way that can be reshared?

function debounce(func, wait, immediate) {
'use strict';
var timeout;
wait = (typeof wait !== 'undefined') ? wait : 20;
immediate = (typeof immediate !== 'undefined') ? immediate : true;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) {
func.apply(context, args);
}
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) {
func.apply(context, args);
}
};
}

Copy link
Author

Choose a reason for hiding this comment

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

Hi @renatonascalves

I've implemented the changes:

  1. Updated the debounce function to ensure consistency with existing code.
  2. Added functionality to skip AJAX requests if the query is empty.

Please review the latest commit and let me know if there are any further adjustments needed.

Copy link
Member

Choose a reason for hiding this comment

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

I actually meant that we could reuse the same code in both places.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the clarification. I understand your suggestion to reuse the same debounce function across different parts of the project. To achieve this, I propose extracting the debounce function from buddypress/src/bp-templates/bp-nouveau/js/buddypress-priority-menu.js and placing it in a shared location that can be imported where needed.

Specifically, we can create a common utility file, utils.js, where we define the debounce function. Then, we can update both mentions.js and buddypress-priority-menu.js to import and use the debounce function from utils.js. This approach ensures consistency and reduces redundancy, as any updates to the debounce function will only need to be made in one place.

Would you agree with this approach? If so, could you please advise on the best location to place this shared function? I'll go ahead and implement these changes once I have your approval.

Copy link
Member

@renatonascalves renatonascalves Aug 9, 2024

Choose a reason for hiding this comment

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

@imath Do you have suggestions for the location of this shared util file?

@renatonascalves
Copy link
Member

A trac ticket would be great too.

- Modified the debounce function to include an option for immediate execution of the function.
- Updated the remoteFilter to skip AJAX requests when the query is empty, ensuring that no unnecessary network calls are made.
Comment on lines 32 to 34
var timeout;
wait = (typeof wait !== 'undefined') ? wait : 20;
immediate = (typeof immediate !== 'undefined') ? immediate : true;
Copy link
Member

Choose a reason for hiding this comment

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

Looks like there is some spacing issues here.

Copy link
Author

Choose a reason for hiding this comment

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

I've updated the spacing issues.

Comment on lines 213 to 215
if (!query.trim()) {
render_view([]); // Return an empty array to indicate no results
return;
Copy link
Member

Choose a reason for hiding this comment

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

Here too.

@renatonascalves
Copy link
Member

renatonascalves commented Aug 27, 2024

@Praveenkroot Let's not try to reuse this functionality. It might be harder than I expected. And might require a large conversation. We can fix the issue here with the current implementation.

Can you create a ticket https://buddypress.trac.wordpress.org/newticket related to this issue?

…alization

* Standardized indentation and spacing in the variable initialization section.
* Corrected inconsistent indentation and spacing in the AJAX request handling code.
@Praveenkroot
Copy link
Author

@renatonascalves Thank you for the feedback. I understand that reusing the functionality might be complex and could require extensive discussion. I’ll focus on addressing the issue with the current implementation as you suggested.

I will create a ticket on BuddyPress Trac regarding this issue and provide a link to it here.

Thanks for your guidance!

@Praveenkroot Praveenkroot deleted the patch-1 branch August 29, 2024 08:44
@Praveenkroot Praveenkroot restored the patch-1 branch August 29, 2024 08:45
@Praveenkroot Praveenkroot reopened this Aug 29, 2024
@renatonascalves
Copy link
Member

@Praveenkroot I think this pr is only pending a ticket with more information and possibly some testing steps.

@Praveenkroot
Copy link
Author

Praveenkroot commented Sep 28, 2024

Hi @renatonascalves,

I've created the Trac ticket as requested, which includes additional details and testing steps for the issue we discussed.

You can find the ticket here: https://buddypress.trac.wordpress.org/ticket/9239#ticket

Please let me know if there’s anything else you'd like to see added or adjusted.

Thanks again for your guidance and feedback!

@renatonascalves
Copy link
Member

Thanks @Praveenkroot 🤟🏽

dcavins pushed a commit to dcavins/buddypress-wp-svn that referenced this pull request Mar 21, 2025
…ormance.

This change aims to optimize the performance of the `@mentions` feature by minimizing unnecessary AJAX requests and improving overall responsiveness.

Props praveenkumar98.

Fixes #9239
Closes buddypress/buddypress#350

git-svn-id: http://buddypress.svn.wordpress.org/trunk@14098 cdf35c40-ae34-48e0-9cc9-0c9da1808c22
emaralive pushed a commit to emaralive/omt-buddypress-sync that referenced this pull request Nov 2, 2025
…ormance.

This change aims to optimize the performance of the `@mentions` feature by minimizing unnecessary AJAX requests and improving overall responsiveness.

Props praveenkumar98.

Fixes #9239
Closes buddypress/buddypress#350

git-svn-id: https://buddypress.svn.wordpress.org/trunk@14098 cdf35c40-ae34-48e0-9cc9-0c9da1808c22
emaralive pushed a commit to emaralive/buddypress-sync that referenced this pull request Nov 3, 2025
…ormance.

This change aims to optimize the performance of the `@mentions` feature by minimizing unnecessary AJAX requests and improving overall responsiveness.

Props praveenkumar98.

Fixes #9239
Closes buddypress/buddypress#350

git-svn-id: https://buddypress.svn.wordpress.org/trunk@14098 cdf35c40-ae34-48e0-9cc9-0c9da1808c22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants