# A concise framerate limiter?

**URL:** <https://forum.babylonjs.com/t/a-concise-framerate-limiter/21039>\
**Category:** Questions\
**Created:** [May 23, 2021, 2:12am UTC](https://forum.babylonjs.com/t/a-concise-framerate-limiter/21039 "2021-05-23T02:12:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sokorototo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sokorototo/32/11061_2.png) [@sokorototo](https://forum.babylonjs.com/u/sokorototo)\
**Post date:** [May 23, 2021, 2:12am UTC](https://forum.babylonjs.com/t/a-concise-framerate-limiter/21039/1 "2021-05-23T02:12:11Z")

</div>

Is there a proper way of limiting the **framerate**? I was thinking of writing my own solid implementation on top of `engine.runRenderLoop` then submitting a pull request to the official repo. The API _may_ look like this:

```javascript
let engine = new BABYLON.Engine(canvas);
let scene = new BABYLON.Scene(engine);

let maxFramerate = 24;
engine.runRenderLoop(() => scene.render(), maxFramerate);

engine.setMaxFramerate(60);
engine.getMaxFramerate();

```

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [May 23, 2021, 2:30pm UTC](https://forum.babylonjs.com/t/a-concise-framerate-limiter/21039/2 "2021-05-23T14:30:36Z")

</div>

There is targetFrameRate getter/setter - [SceneOptimizer | Babylon.js Documentation](https://doc.babylonjs.com/typedoc/classes/babylon.sceneoptimizer#targetframerate)

Also, extremely interesting article - [How we improved our 3D web viewer performance](https://www.naker.io/posts/3-tricks-on-how-we-improved-by-10-our-3d-web-viewer-performance)

From the old forum - [Limit frames per second - Questions & Answers - HTML5 Game Devs Forum](https://www.html5gamedevs.com/topic/2287-limit-frames-per-second/)  
And even special limitLoop.js - [Limit the frame-rate being targeted with requestAnimationFrame · GitHub](https://gist.github.com/addyosmani/5434533)

---

<div class="post-metadata">

**Author:** ![br-matt](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/br-matt/32/22436_2.png) [@br-matt](https://forum.babylonjs.com/u/br-matt)\
**Post date:** [May 23, 2021, 2:53pm UTC](https://forum.babylonjs.com/t/a-concise-framerate-limiter/21039/3 "2021-05-23T14:53:49Z")

</div>

@labris I find the SceneOptimizer does not kick in very often when set at 30 FPS, 1000ms check rate. Even when users on lower end machines are getting slide-show experiences sometimes. Do you have any tips for what I could be doing wrong with it ?

---

<div class="post-metadata">

**Author:** ![sokorototo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sokorototo/32/11061_2.png) [@sokorototo](https://forum.babylonjs.com/u/sokorototo)\
**Post date:** [May 24, 2021, 12:22am UTC](https://forum.babylonjs.com/t/a-concise-framerate-limiter/21039/4 "2021-05-24T00:22:25Z")

</div>

Try using a check rate of 250ms.

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [May 24, 2021, 11:37am UTC](https://forum.babylonjs.com/t/a-concise-framerate-limiter/21039/5 "2021-05-24T11:37:39Z")

</div>

As far as I know SceneOptimizer will try to make desired FPS but it doesn’t mean that every user will get it, especially at lower end.

---

<div class="post-metadata">

**Author:** ![br-matt](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/br-matt/32/22436_2.png) [@br-matt](https://forum.babylonjs.com/u/br-matt)\
**Post date:** [May 24, 2021, 1:40pm UTC](https://forum.babylonjs.com/t/a-concise-framerate-limiter/21039/6 "2021-05-24T13:40:16Z")

</div>

> Try using a check rate of 250ms.

I will give that a shot ! Edit: Wow 250ms is working way more reliably, thank you for helping me in your own thread 🚀

> As far as I know SceneOptimizer will try to make desired FPS but it doesn’t mean that every user will get it, especially at lower end.

That would be magical indeed if it could just fix anyone 🙂 🙏 It’s 50/50 on laggy systems wether the first/subsequent optimizations gets applied. I went about setting it up the way the docs outline with custom functions for different priority levels, including some logging in there as well.
