Skip to content
This repository has been archived by the owner on Apr 19, 2021. It is now read-only.

changed debounce out for bindRaf #14

Merged
merged 3 commits into from Nov 13, 2017
Merged

changed debounce out for bindRaf #14

merged 3 commits into from Nov 13, 2017

Conversation

falconmick
Copy link
Contributor

the debounce that was setup for calling on scroll/resize was 10ms. All browsers that I know of set a FPS cap of 60fps. 1s/60f = 16ms.

So for two reasons I have changed to bindRaf

  1. There is no reason to check if we are below the desired point to change the element to visable until we are rendering a new frame, now if the fps tanks to say 25fps the calculations will only call once every 40ms, so 1/4 the calcs when the system is struggling.
  2. setTimeout is not guarinteed to call on the time specified. This caused me issues because on my semi-large site, when I scroll, Chrome decides that my timeout callback is less important than the scroll keeping smooth. See https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout#Reasons_for_delays_longer_than_specified

I came to the conclution that this was required after reading this SOF answer: https://stackoverflow.com/questions/41740082/scroll-events-requestanimationframe-vs-requestidlecallback-vs-passive-event-lis

Also I have not followed your current standards for code, instead of functions I've used ES6 syntax aswell as not using default exports on library functions. If you don't like this feel free to change it around, but this is how I coded it when I was looking for a solution to my problem.

Cheers.

falconmick and others added 2 commits October 5, 2017 13:53
pull latest from main repo
the debounce that was setup for calling on scroll/resize was 10ms. All browsers that I know of set a FPS cap of 60fps. 1s/60f = 16ms.

So for two reasons I have changed to bindRaf
1. There is no reason to check if we are below the desired point to change the element to visable until we are rendering a new frame, now if the fps tanks to say 25fps the calculations will only call once every 40ms, so 1/4 the calcs when the system is struggling.
2. setTimeout is not guarinteed to call on the time specified. See https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout#Reasons_for_delays_longer_than_specified

I came to the conclution that this was required after reading this SOF answer: https://stackoverflow.com/questions/41740082/scroll-events-requestanimationframe-vs-requestidlecallback-vs-passive-event-lis

Cheers.

return () => {
that = this;
args = arguments;
Copy link
Owner

Choose a reason for hiding this comment

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

not using these variables, so could probably make this simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will try to get around to fixin that tomorrow, sorry I tried a few solution before I was happy, that’s the left over mess

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dazld Just read over the code. both variables are used.

  1. self is used so that on callback the context of the function we are wrapping has the same context as the place we bound it (I think)
  2. args is used to grab the arguments that were passed in upon creation of the callback.

I have made another commit to rename that to the more standard self name and removed debounce as it's unused.

The only improvement this could use would be a cancellation token, but I think that can wait as the not binding to null fix previously is good enough.

Any more issues?

Copy link
Owner

Choose a reason for hiding this comment

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

I should learn to read, sorry @falconmick - no, this all looks ok.


# for now removing package-lock as it's not my decision to include
package-lock.json
Copy link
Owner

Choose a reason for hiding this comment

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

good point, we should pin this somehow.

@@ -2,12 +2,12 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import debounce from './lib/debounce';
import { bindRaf } from "./lib/bindRaf";
Copy link
Owner

@dazld dazld Oct 13, 2017

Choose a reason for hiding this comment

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

could you use kebab case for file names?

@@ -1,9 +0,0 @@
export default function debounce(fn, time) {
Copy link
Owner

Choose a reason for hiding this comment

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

could probably use this as a fallback for browsers that don't have rAF, so maybe keep it around.

@dazld
Copy link
Owner

dazld commented Nov 13, 2017

tested it locally, seems good to go!

@dazld dazld merged commit 95020ab into dazld:master Nov 13, 2017
@dazld
Copy link
Owner

dazld commented Nov 13, 2017

published as 1.3.0!

@falconmick
Copy link
Contributor Author

falconmick commented Nov 14, 2017 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants