Stats.js Performance Monitor
Stats.js is a lightweight, client-side JavaScript performance monitor designed to display real-time metrics such as Frames Per Second (FPS), milliseconds per frame (MS), and allocated memory (MB) directly in the browser. The current stable version, 0.17.0 (r17), introduced a significant change by converting the library to an ECMAScript module. While its release cadence has historically been irregular, with key updates like `r16` adding `devicePixelRatio` support and `r15` reverting to a more efficient canvas-based rendering, `r17` represents the most recent major shift in its distribution format. It differentiates itself through its minimal footprint and immediate visual feedback, making it an excellent tool for quick performance profiling during development without requiring complex tooling integrations.
Common errors
-
Uncaught ReferenceError: Stats is not defined
cause Attempting to use `Stats` globally after upgrading to v0.17.0 (r17) without using ES module imports.fixChange your code from `var stats = new Stats();` to `import Stats from 'stats.js'; const stats = new Stats();`. -
require is not defined
cause Trying to use `require('stats.js')` in an environment that only supports ES modules or in a browser without a CommonJS bundler.fixIf using v0.17.0 or newer, use `import Stats from 'stats.js';`. If targeting older versions in a browser, ensure `stats.js` is loaded via a `<script>` tag or a compatible bundler. -
TypeError: Cannot read properties of undefined (reading 'dom')
cause The `stats` object was not properly initialized or is `undefined` when trying to access `stats.dom`.fixEnsure `new Stats()` is called and assigned to the `stats` variable before attempting to access its properties. Check for correct `import Stats from 'stats.js';`.
Warnings
- breaking Version 0.17.0 (r17) converted the library to an ES module. This change breaks existing CommonJS `require()` or global `Stats` usage.
- gotcha The 'MB' (memory) panel often requires specific browser flags to function correctly and provide precise memory information, particularly in Chrome.
- gotcha Older versions (r3, r15) made changes to default positioning and rendering technology (DOM vs. Canvas). While generally backward compatible, these could subtly affect integration in complex layouts.
- deprecated Direct embedding via a bookmarklet loading from `rawgit.com` might encounter issues as `rawgit.com` is no longer maintained.
Install
-
npm install stats.js -
yarn add stats.js -
pnpm add stats.js
Imports
- Stats
const Stats = require('stats.js'); // CommonJS import var stats = new Stats(); // Global access pre-r17import Stats from 'stats.js'
- Stats
import Stats from 'stats.js/src/Stats'
Quickstart
import Stats from 'stats.js';
const stats = new Stats();
stats.showPanel( 0 ); // 0: fps, 1: ms, 2: mb, 3+: custom
stats.dom.style.position = 'fixed';
stats.dom.style.top = '0px';
stats.dom.style.left = '0px';
stats.dom.style.zIndex = '10000';
document.body.appendChild( stats.dom );
function animate() {
stats.begin();
// Your monitored code goes here
// For example, a simple loop or animation logic
for (let i = 0; i < 100000; i++) {
Math.sin(i);
}
stats.end();
requestAnimationFrame( animate );
}
requestAnimationFrame( animate );