Greasy Fork

YouTube CPU Tamer by AnimationFrame

减少YouTube影片所致的能源消耗

目前为 2021-09-30 提交的版本。查看 最新版本

作者
𝖢𝖸 𝖥𝗎𝗇𝗀
评分
0 0 0
版本
2021.09.30
创建于
2021-08-29
更新于
2021-09-30
大小
8.3 KB
许可证
MIT
适用于

Inspired by kona's YouTube CPU Tamer

Description

This is for all kinds of YouTube applications, including main page, embedded video, live chat, and YouTube Music.

- Faster
- More Stable
- Lower Battery Consumption

Note1: This hijacks Web APIs: setTimeout, setInterval, clearTimeout, and clearInterval
Note2: This uses setInterval(..., 250ms) instead of requestAnimationFrame for background running.
Note3: If Timer Throttling2 occurs in background running, the interval would be increased, say 1000ms.


Remarks - Calculation of nextAt

if(o.nextAt + _interval > now) o.nextAt += _interval;
else if(o.nextAt + 2*_interval > now) o.nextAt += 2*_interval;
else if(o.nextAt + 3*_interval > now) o.nextAt += 3*_interval;
else if(o.nextAt + 4*_interval > now) o.nextAt += 4*_interval;
else if(o.nextAt + 5*_interval > now) o.nextAt += 5*_interval;
else o.nextAt = now + _interval;

o.nextAt = now + _interval for now >= o.nextAt + 5*_interval
t1 >= t0 + 6*T
t1 - t0 >= 6T
t1 - t0 <= 1000 (Timer Throtting)
1000 >= 6T
T <= 1000/6 = 166.7 ms

o.nextAt = now + _interval mostly for T < 167ms for background running with timer throtting

Remarks - Native Behavior Change

This userscript hijacks setTimeout & setInterval leading different browser behaviors as follows:

Case 1A (Active Page)

let f=function(){console.log('hello world')};
setTimeout(f,100);setTimeout(f,100);setTimeout(f,100);

Native: print out "hello world" 3 times.
Modified: print out "hellow world" 1 time. (in the same AnimationFrame)

Case 1B (Active Page)

let f=function(){console.log('hello world')};
setTimeout(f,100);setTimeout(f,110);setTimeout(f,120);

Native: print out "hello world" 3 times.
Modified: print out "hellow world" 1 ~ 3 time(s) depending on the execution time of function handler, the responsiveness of browser, and the segregation of AnimationFrames;

Case 1C (Active Page)

let f=function(){console.log('hello world')};
setTimeout(f,100);setTimeout(f,200);setTimeout(f,300);

Native: print out "hello world" 3 times.
Modified: print out "hello world" 3 times3. (in 3 AnimationFrames)

Case 2A (Active Page)

clearInterval(window.f1s); clearInterval(window.f2s); let w1=0,w2=0;let f1 = ()=>{w1++}, f2= ()=>{w2++}; window.f1s=setInterval(f1,70); window.f2s=setInterval(f2, 140); setTimeout(()=>{console.log(w1,w2);},3000)
Native: print out "42 21".
Modified: print out "42 21"3.

Case 2B (Background Application)

clearInterval(window.f1s); clearInterval(window.f2s); let w1=0,w2=0;let f1 = ()=>{w1++}, f2= ()=>{w2++}; window.f1s=setInterval(f1,70); window.f2s=setInterval(f2, 140); setTimeout(()=>{console.log(w1,w2);},3000)
Native (Timer Throttling2): print out values between 3 and 4. (e.g. "4 4", "4 3")
Modified, if no Timer Throttling: print out values between 12 and 13 (e.g. "13 12", "13 13", "12 12", "13 12").
Remarks: 3000/250 = 12

Case 3A (Active Page)

clearInterval(window.f1s); clearInterval(window.f2s); let w1=0,w2=0;let f1 = ()=>{w1++}, f2= ()=>{w2++}; window.f1s=setInterval(f1,5); window.f2s=setInterval(f2, 10); setTimeout(()=>{console.log(w1,w2);},3000)
Native: print out "600 300"1.
Modified: print out "600 301"3 or "600 300"3.

Case 3B (Background Application)

clearInterval(window.f1s); clearInterval(window.f2s); let w1=0,w2=0;let f1 = ()=>{w1++}, f2= ()=>{w2++}; window.f1s=setInterval(f1,5); window.f2s=setInterval(f2, 10); setTimeout(()=>{console.log(w1,w2);},3000)
Native (Timer Throttling2): print out values between 3 and 4. (e.g. "4 4", "4 3")
Modified, if no Timer Throttling: print out values between 12 and 13 (e.g. "13 12", "13 13", "12 12", "13 12").
Remarks: 3000/250 = 12

1 Actual values could be smaller due to your UI/network activity.
2 Timer Throttling is the browser's native behavior on background applications.
3 Actual values could be much smaller due to your UI/network activity.

Disclaimer: The suggested results and behaviors are for reference only. They are not guaranteed and depend on your OS and browser.

Further Reading - Timer Throttling

What Happens to setTimeout() / setInterval() Timers Running on Inactive Browser Tabs ?
Heavy throttling of chained JS timers beginning in Chrome 88

Sample Testing Links

Also see...