Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use 'requestIdleCallback' when available for debounce/throttle #1352

Closed
wants to merge 4 commits into from

Conversation

msssk
Copy link
Contributor

@msssk msssk commented Feb 9, 2017

Fixes #1351

@msssk
Copy link
Contributor Author

msssk commented Feb 9, 2017

This is a severe problem in affected configurations, so I cherry-picked this commit and created a PR for 0.4 as well.

To reproduce, simply scroll through an OnDemandGrid with Chrome 56. Scroll rapidly using the mousewheel. Normal behavior is you see the grid is blank while scrolling, but as soon as you stop it is almost instantly populated with rows. New behavior in Chrome 56 is that there is a significant delay before the rows are rendered. If pagingMethod is set to throttle the rows are never rendered.

This may be dependent on the grid complexity - I have a test case I can share on request.

It is likely that a dojo/has test for requestIdleCallback will be added to core Dojo at some point, but this issue is severe enough that I think we should release a dgrid fix ASAP.

Also, note that this is probably a less than optimal fix. We are basically overriding the browser's attempted performance optimization and saying "run this code anyway". It's up to the developer employing dgrid to test performance, notice issues, and adjust application configuration (including dgrid options) to alleviate them.

If we fully follow the intended use of requestIdleCallback we would have to make OnDemandList#_processScroll aware of its resource usage, and have it yield when appropriate while saving state so it can resume later.

Copy link
Member

@dylans dylans left a comment

Choose a reason for hiding this comment

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

It's unfortunate we have to duplicate the full blocks of code for what is really 1-2 lines of different code based on which method to follow. Is that because we effectively call the methods within the setTimeout and requestIdleCallback, or could the has statements just wrap the 1-2 lines per method that deviate between implementations?

Note, after this lands, it's time for 1.12.0, 1.11.x, 1.10.x, and 0.4.x releases!

@bryanforbes
Copy link
Member

Couldn't we abstract the decision between setTimeout and requestIdleCallback earlier in the module and use the abstraction instead? For instance:

var delayCallback;
var cancelDelay;

if (has('requestidlecallback')) {
    delayCallback = function (callback, delay) {
        return requestIdleCallback(callback, { timeout: delay });
    };
    cancelDelay = cancelIdleCallback;
}
else {
    delayCallback = setTimeout;
    cancelDelay = clearTimeout;
}

Then the implementations of throttle, throttleDelayed, and debounce would use delayCallback() and cancelDelay().

@msssk
Copy link
Contributor Author

msssk commented Feb 9, 2017

I'm sold on Bryan's suggestion - performance impact of a single extra layer of function invocation is likely to be negligible. PR updated.

@edhager edhager closed this in ccfcbc1 Feb 10, 2017
@edhager
Copy link
Contributor

edhager commented Feb 10, 2017

Thanks @msssk!

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