What this isn't:
You'll get the most out of this talk if you:
Get the source code
For when you need to move data from the backend to the frontend with no latency
credit: Marshall Ruse
react-use-websocket
react-use-websocket-lite
requestAnimationFrame?
setInterval(()=>draw(),1000/60)
requestAnimationFrame(draw)
setTimeout/setInterval: macrotasks (last)promise: microtask (first)requestAnimationFrame: special microtask (first-er: cuts in line before microtasks if it's time to repaint)function draw() {
//animation logic goes here
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
useEffect(() => {
let animationFrameId;
const draw = () => {
//animation logic here
animationFrameId = requestAnimationFrame(draw);
};
draw();
return () => {
cancelAnimationFrame(animationFrameId);
};
},[]);
render
render
const Canvas = ({props})=>{
const canvasRef = useRef(null);
const [context, setContext] = useState(null);
useEffect(() => {
if (canvasRef.current) {
setContext(canvasRef.current.getContext("2d"));
}
}, []);
return <canvas ref={canvasRef}>;
}
<canvas> element alsoconst myPoints = [{x:10,y:220},{x:125,y:10},{x:250,y:220}];
ctx.strokeStyle="lightblue";
ctx.beginPath();
ctx.lineTo(250,220);
for (const point of myPoints){
ctx.lineTo(point.x,point.y);
}
ctx.stroke();let myPoints = [{x:10,y:220},{x:125,y:10},{x:250,y:220}]
const draw = ()=>{
ctx.clearRect(0,0,300,300);
const offsetX = 1*Math.random()>0.5?-1:1;
const offsetY = 1*Math.random()>0.5?-1:1;
ctx.beginPath();
for (const point of myPoints){
point.x+=offsetX;
point.y+=offsetY;
ctx.lineTo(point.x,point.y);
}
ctx.stroke();
animationFrameId = requestAnimationFrame(draw);
}
let offsetX =1;
let offsetY = 1;
const draw = ()=>{
ctx.clearRect(0,0,300,300);
const myPoints = [{x:10,y:150},{x:85,y:10},{x:175,y:150}]
ctx.beginPath();
for (const point of myPoints){
point.x+=offsetX;
point.y+=offsetY;
ctx.lineTo(point.x,point.y);
if (point.x<=0 ||point.x>=300){
offsetX*=-1;
}
if (point.y<=0||point.y>=300){
offsetY*=-1;
}
}
ctx.stroke();
animationFrameId = requestAnimationFrame(draw);
}
https://dev.to/georgekobaidze/backend-engineer-me-ships-a-browser-game-with-one-unintentional-system-requirement-my-monitor-1ojd
const draw = () => {
xOffset+=0.5*offset;
ctx.clearRect(0,0,300,300)
//draw the face: not shown for brevity
//draw the nose:
ctx.beginPath();
ctx.lineWidth=8;
ctx.lineCap="round";
ctx.moveTo(75, 75)
ctx.lineTo(75+xOffset,75)
ctx.stroke();
ctx.closePath();
if (xOffset>100 || xOffset<0 ){
offset*=-1;
}
}
const firstFrame = ()=>{
//draw the face (not shown), then kick off the RAF
requestAnimationFrame(draw);
}
const draw = () => {
xOffset+=0.5*offset;
ctx.clearRect(70,70,xOffset,10) //clears a box starting
//at coordinate (70,70) of size (xOffset,10)
//draw the nose:
ctx.beginPath();
ctx.lineWidth=8;
ctx.lineCap="round";
ctx.moveTo(75, 75)
ctx.lineTo(75+xOffset,75)
ctx.stroke();
ctx.closePath();
if (xOffset>100 || xOffset< 0 ){
offset*=-1;
}
firstFrame()
}
useEffect(() => {
const draw = () => {
if (facesRef.current) {
context.clearRect(0, 0, canvasWidth, canvasHeight);
context.beginPath();
for (const face_point of facesRef.current) {
context.lineTo(
face_point.x,
face_point.y,
);
context.stroke();
}
}
}
};
let animationFrameId;
const renderAndRequestNewFrame = () => {
draw();
sendMessage("{'sendNext':'frame'}");
animationFrameId = requestAnimationFrame(renderAndRequestNewFrame);
};
renderAndRequestNewFrame();
return () => {
cancelAnimationFrame(animationFrameId);
}, [facesRef, context, sendMessage]);
componentShouldUpdate in the days of class componentsconst MyComponent = ({props})=>{
/*some very cool stuff here*/
return (<>Very Cool Component {props.name}<>)
}
export default React.memo(MyComponent);
myArray.forEach(item=>doSomethingWith(item));
//is SIGNIFICANTLY slower than
for (const item of myArray){
doSomethingWith(item)
}
Some benchmarks put for..of as more than twice as fast as .forEach.
(I tried my own benchmark and forEach crashed my computer 🤷♀️)
.time() and .timeEnd()console.time("my expensive operation")
//do something expensive
console.timeEnd("my expensive operation")
produces...
my expensive operation: 15801ms - timer ended
Contact me

Download slides/source code
