DEV Community

Abdul Majeed
Abdul Majeed

Posted on

How I Built a Simple and Accurate FPS Calculator for Gamers and Developers


Ever wondered why your game feels laggy even with a good PC?

I used to face this problem a lot. The game looked fine, but the performance felt off. That is when I realized I needed a simple way to measure FPS in real time.

So I built my own fps-calculator to understand performance better. In this post, I will show you how it works and how you can build one too.

What is an FPS Calculator

An FPS calculator measures how many frames your system renders per second.

  • Higher FPS means smoother gameplay
  • Lower FPS means lag and stuttering It is one of the most important metrics in gaming and performance testing.

Why I Built My Own FPS Calculator

Most tools are either too complex or not flexible.

I wanted something:

  • Simple to use
  • Lightweight
  • Easy to integrate into any project
  • Useful for both developers and gamers That is why I created a custom solution.

How FPS is Calculated

FPS is calculated using time between frames.

Basic idea:

Measure time between two frames
Convert that into frames per second

Formula:

FPS = 1 / Delta Time

Where delta time is the time taken to render one frame.

Building a Simple FPS Calculator in JavaScript

Here is a working example you can use directly.

Step 1: Basic Setup
let lastFrameTime = performance.now();
let fps = 0;

function calculateFPS() {
const now = performance.now();
const delta = (now - lastFrameTime) / 1000; // convert ms to seconds

fps = 1 / delta;
lastFrameTime = now;

requestAnimationFrame(calculateFPS);
}

calculateFPS();
Step 2: Display FPS on Screen
const fpsDisplay = document.createElement("div");

fpsDisplay.style.position = "fixed";
fpsDisplay.style.top = "10px";
fpsDisplay.style.left = "10px";
fpsDisplay.style.padding = "5px 10px";
fpsDisplay.style.background = "black";
fpsDisplay.style.color = "white";
fpsDisplay.style.fontSize = "14px";

document.body.appendChild(fpsDisplay);

function updateFPS() {
fpsDisplay.textContent = FPS: ${fps.toFixed(2)};
requestAnimationFrame(updateFPS);
}

updateFPS();
Final Output
Real time FPS counter
Lightweight
Works in any browser
Improving Accuracy

Basic FPS calculation can be unstable. Here is how to improve it:

Use averaging over multiple frames
Avoid sudden spikes
Smooth the values
Example with Averaging
let frameTimes = [];

function calculateFPS() {
const now = performance.now();
const delta = now - lastFrameTime;
lastFrameTime = now;

frameTimes.push(delta);

if (frameTimes.length > 60) {
frameTimes.shift();
}

const average = frameTimes.reduce((a, b) => a + b, 0) / frameTimes.length;
fps = 1000 / average;

requestAnimationFrame(calculateFPS);
}
Use Cases of FPS Calculator

This tool is not just for gamers.

  • Game developers to optimize performance
  • Web developers working with animations
  • PC users testing hardware performance
  • Benchmarking different settings
  • Try a Ready-Made Tool

If you do not want to build from scratch, you can try this simple tool:

Use this fps-calculator here:
https://coreoptimize.com/fps-calculator/

It helps you quickly estimate and analyze your FPS performance.

  • Common Mistakes to Avoid
  • Using instant FPS instead of averaged values
  • Ignoring hardware limitations
  • Not testing in real scenarios
  • Overloading the UI with heavy scripts

Key Takeaways

  • FPS is critical for smooth performance
  • A simple calculator can give powerful insights
  • JavaScript makes it easy to build one
  • Averaging improves accuracy
  • Useful for both gaming and development Final Thoughts

Building this fps calculator helped me understand performance in a practical way. It is simple, but very useful when you want real insights.

If you are working on games, animations, or performance tools, I highly recommend building one yourself.

Top comments (0)