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

Minor optimization - don't call setTimeout if not needed #43

Closed
dormeiri opened this issue Dec 4, 2023 · 2 comments · Fixed by #46
Closed

Minor optimization - don't call setTimeout if not needed #43

dormeiri opened this issue Dec 4, 2023 · 2 comments · Fixed by #46

Comments

@dormeiri
Copy link
Contributor

dormeiri commented Dec 4, 2023

When getDelay() returns 0 there is no need to call setTimeout, it creates redundant event-loop event.

https://github.com/sindresorhus/p-throttle/blob/3dffe707c0061370be8ce0a8c687bd032b6acaef/index.js#L83-85

My suggestion:

  • Check first if the result of getDelay() is greater than 0
  • If yes, run setTimeout and insert it to the queue.
  • Otherwise, run execute directly, call queue.delete(timeoutId); only if timeoutId is defined.

WDYT?

I can open a PR if it helps.

@sindresorhus
Copy link
Owner

👍

I don't think it will help much in real-world situations though, as the bottleneck is IO not the this package or the event-loop.

@dormeiri
Copy link
Contributor Author

dormeiri commented Dec 5, 2023

I generally agree, however it is more for when there are many events in the event-loop, then the execution start will get delayed.

Here is a PR #46

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 a pull request may close this issue.

2 participants