Gathering detailed insights and metrics for raf-schd
Gathering detailed insights and metrics for raf-schd
Gathering detailed insights and metrics for raf-schd
Gathering detailed insights and metrics for raf-schd
A throttle function that uses requestAnimationFrame to rate limit
npm install raf-schd
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
453 Stars
49 Commits
23 Forks
5 Watching
13 Branches
8 Contributors
Updated on 12 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-5.2%
411,266
Compared to previous day
Last week
5.5%
2,276,203
Compared to previous week
Last month
3.5%
9,304,899
Compared to previous month
Last year
13.8%
101,295,401
Compared to previous year
A throttle
function that uses requestAnimationFrame
to limit the rate at which a function is called.
For background information on rate limiting functions, see Rate limiting functions from scratch
1import rafSchd from 'raf-schd'; 2 3const expensiveFn = arg => { 4 //... 5 console.log(arg); 6}; 7 8const schedule = rafSchd(expensiveFn); 9 10schedule('foo'); 11schedule('bar'); 12schedule('baz'); 13 14// animation frame fires 15 16// => 'baz'
raf-schd
supports the use case where you want to limit the rate at which your functions are called based on requestAnimationFrame
. Unlike a standard throttle
function, raf-schd
uses requestAnimationFrame
to rate limit, rather than waiting a fixed amount of time. Using requestAnimationFrame
for throttling is powerful because the browser will automatically reduce the amount of frames provided based on the available resources. So if the browser only provides 30fps then your throttled function will only be called 30 times.
raf-schd
is an extremely useful performance utility.
raf-schd
Optimised scroll listener example taken from MDN
1var last_known_scroll_position = 0; 2var ticking = false; 3 4function doSomething(scroll_pos) { 5 // do something with the scroll position 6} 7 8window.addEventListener('scroll', function(e) { 9 last_known_scroll_position = window.scrollY; 10 if (!ticking) { 11 window.requestAnimationFrame(function() { 12 doSomething(last_known_scroll_position); 13 ticking = false; 14 }); 15 } 16 ticking = true; 17});
raf-schd
1import rafSchd from 'raf-schd'; 2 3function doSomething(scroll_pos) { 4 // do something with the scroll position 5} 6 7const schedule = rafSchd(doSomething); 8 9window.addEventListener('scroll', function() { 10 schedule(window.scrollY); 11});
At the top level raf-schd
accepts any function a returns a new ResultFn
(a function that wraps your original function).
The ResultFn
will execute your function with the latest arguments provided to it on the next animation frame.
1import rafSchd from 'raf-schd'; 2 3const doSomething = () => {...}; 4 5const schedule = rafSchd(doSomething); 6 7schedule(1, 2); 8schedule(3, 4); 9schedule(5, 6); 10 11// animation frame fires 12 13// do something called with => '5, 6'
.cancel
raf-schd
adds a .cancel
property to the ResultFn
so that it can be easily cancelled. The frame will only be cancelled if it has not yet executed.
1const schedule = rafSchd(doSomething); 2 3schedule('foo'); 4 5schedule.cancel(); 6 7// now doSomething will not be executed in the next animation frame
rafSchedule
1type rafSchedule = (fn: Function) => ResultFn; 2 3// Adding a .cancel property to the WrapperFn 4 5type WrapperFn = (...arg: mixed[]) => void; 6type CancelFn = {| 7 cancel: () => void, 8|}; 9type ResultFn = WrapperFn & CancelFn;
If you want to really ensure that your code is working how you intend it to - use raf-stub
to test your animation frame logic.
1# yarn 2yarn add raf-schd 3 4# npm 5npm install raf-schd --save
1import rafSchd from 'raf-schd';
If you are in a CommonJS environment (eg Node), then you will need add .default
to your import:
1const rafSchd = require('raf-schd').default;
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 6/28 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
22 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More