Performance Issue: setTimeout/setInterval
See original GitHub issueIt makes more sense to use setTimeout instead of setInterval.
If this is done, the function can determine when the text is actually going to change and not waste calls that don’t do anything. If the text says 2 years ago
, it’s probably not going to change in a minute, so it makes more sense to calculate the time delay.
Also, this would be more efficient for a modified version that shows %d seconds ago
and needs to update at 1000 ms intervals only before it becomes a minute. Then it can change the interval to 60000 ms.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:5
- Comments:7
Top Results From Across the Web
does setTimeout() affects performance - Stack Overflow
No significant effect at all, setTimeout runs in an event loop, it doesn't block or harm execution. – Benjamin Gruenbaum. Oct 24, 2013...
Read more >Angular — Performance issue caused by setTimeout() and ...
From my experience, one of the most easily missed causes of the second one is timer, which means setTimeout() and setInterval() . Too...
Read more >Scheduling: setTimeout and setInterval
setInterval allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval.
Read more >The curious case of performance testing setTimeout(0)
setInterval runs the function repeatedly, waiting 50 ms between each run, to make sure the stack is clear. This is inelegant, but tests...
Read more >setTimeout() - Web APIs | MDN
Timeouts are cancelled using clearTimeout() . To call a function repeatedly (e.g., every N milliseconds), consider using setInterval() . Non- ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Here’s a version (based on a slightly older base) which does this, and also doesn’t keep the timer running if the element is no longer in the DOM.
Unless you can tolerate error of up to 1 year, disregard what I said about grouping timers, as
2 years ago
for one timer could be supposed to update a few second after another timer that says the same. Updating all of the “year” timers at the same time could be more efficient (less timers), but less accurate.The timeago.org plugin updates the timer
1 second afterimmediately when it’s supposed to change.It makes sense to set the timer like this (but maybe 1 ms after the time instead of 1 entire second).https://github.com/hustcc/timeago.js/blob/310864d574c89cb692c0395dd7fe650cfa12fba7/src/timeago.js#L80-L99