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

Refactor utils #8360

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Refactor utils #8360

wants to merge 7 commits into from

Conversation

ahmed0saber
Copy link

No description provided.

@welcome
Copy link

welcome bot commented Jul 15, 2023

💖 Thanks for opening this pull request! 💖

Things that will help get your PR across the finish line:

  • Run npm run lint -- --errors locally to catch formatting errors earlier.
  • Include tests when adding/changing behavior.
  • Include screenshots and animated GIFs whenever possible.

We get a lot of pull requests on this repo, so please be patient and we will get back to you as soon as we can.

if (end > duration) {
end = duration;
}
end = Math.min(end, duration);
Copy link
Contributor

Choose a reason for hiding this comment

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

This tries to redefine a const

Copy link
Author

Choose a reason for hiding this comment

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

That's unbelievable mistake, I've fixed it.

@@ -60,19 +60,15 @@ export const bind_ = function(context, fn, uid) {
*
* @return {Function}
*/
export const throttle = function(fn, wait) {
let last = window.performance.now();
Copy link
Contributor

Choose a reason for hiding this comment

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

This was changed to use window.performance.now() in #5870. Why do you propose changing it to use Date.now()?

Copy link
Author

@ahmed0saber ahmed0saber Aug 11, 2023

Choose a reason for hiding this comment

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

That's a good question, we use window.performance.now() when we need a high level of accuracy as it is a measurement of floating point milliseconds, but do these 0.153184 milliseconds really matter in our situation? I don't think they matter because we are using 30 milliseconds as a delay for the throttle function which is so huge compared to time % 1, so we can simply neglect these neglectable numbers which have small bad effect on the performance, as window.performance.now() is 3 to 4 times slower than Date.now(), as shown in the following picture:
Screenshot 2023-08-11 162527
Finally, I have initialized lastCallTime with 0, to avoid calling any function that requires a non-zero milliseconds to run.
Also paying some attention to popularity and browser compatibility, we will find that Date.now() is the winner.

Copy link
Member

@gkatsev gkatsev Jan 2, 2024

Choose a reason for hiding this comment

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

The main reason to use performance.now() here isn't the higher resolution but the guarantee of monotonic time updates. I would be inclined to leave it as is due to that.

I'm not sure that the performance difference between Date.now() and performance.now() in this usage are significant enough to worry about.

Copy link
Contributor

@thijstriemstra thijstriemstra Feb 14, 2024

Choose a reason for hiding this comment

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

Perhaps the method used to determine the start time should be configurable, I can imagine performance.now() being superceded by some other better native JS alternative at some point (it never stops, does it) so a method that allows the user to use Date.now() or performance.now() or even nextnewthing.now(), is a reasonable idea, one I should've thought of when I submitted #5870

That being said, please don't change it back to new Date().getTime()!

@@ -141,7 +141,7 @@ function createTimeRangesObj(ranges) {
export function createTimeRanges(start, end) {
if (Array.isArray(start)) {
return createTimeRangesObj(start);
} else if (start === undefined || end === undefined) {
} if (start === undefined || end === undefined) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} if (start === undefined || end === undefined) {
}
if (start === undefined || end === undefined) {

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.

None yet

4 participants