disposable-frames
Disposable frame scheduler which abandons frames if CPU is high or the I/O event loop is blocking. disposable-frames
is useful for web pages with heavy DOM manipulation.
disposable-frames
detects the usage of CPU approximately by monitoring the hehavior of setTimeout(func, 0)
.
Install
$ npm i disposable-frames
Usage
setImmediate(func: Function, options: Object): number | Immediate
- func
Function
The function to call assetTimeout(func, 0)
- options.tolerance
?number=0
The miniseconds within which since thesetImmediate
called that thefunc
is allowed to execute. Ifoptions.tolerance
is0
, the default value, it indicates there is no restriction, which is silly because it is the only reason for this package to exist. - Returns
Immediate
on nodenumber
the timer id on browsers
Schedules the "immediate" execution of the func
if the schedule doesn't take too long.
If we schedule a function foo
with options.tolerance
as 50
(ms), and in the real world, the I/O event loops take too long to respond, which causes that the setImmediate
callback is supposed to be scheduled after 100ms from the beginning. And then the execution of foo
will be abandoned according to options.tolerance
.
immediate(func: Function, options: Object): Function
- func
Function
The function to call assetTimeout(func, 0)
- options
?Object
- tolerance
?number=0
- maxWait
?number=0
The maximum timefunc
is allowed to be abandoned before it's invoked. In the other words, after everymaxWait
time, one execution offunc
is allowed despite of the limitation oftolerance
. IfmaxWait
is 0, the feature is disabled. - leading
boolean=false
Iftrue
, the first execution is always allowed.
- tolerance
Wrap the func
as a new function which schedules func
as well as setImmediate
does every time the wrapper function invokes.
const wrapped =
Suppose that it takes precisely 100ms for func
to execute every time then:
// will execute // disposed // disposed // will execute
License
MIT