Module
Pure Function Ratio
What fraction of this file's functions are free of side effects?
What it measures
Pure Function Ratio (PFR) reports the proportion of functions in a file that tsmetrics classifies as pure — that is, having no detected side effects. Pure functions are easier to test, compose, and reason about.
A PFR of 1.0 means every function in the file is pure. A PFR of 0.0 means every function has at least one side effect indicator.
How it's computed
PFR = pure_functions / total_functions
Defaults to 1.0 when no functions are present.
Impurity indicators
A function is marked impure if any of these patterns are detected in its body:
| Pattern | Example |
|---|---|
Accesses this | this.state = … |
| Calls known impure APIs | console.log(), fetch(), Math.random(), Date.now(), setTimeout() |
| Mutates a parameter property | param.x = 1 |
| Writes to outer-scope variables | outerVar = … |
Uses await | await fetch(…) |
| Calls array mutation methods | arr.push(), arr.splice(), arr.sort() |
Uses delete | delete obj.key |
Example
TypeScript — PFR: 0.5 (1 pure, 1 impure)
// Pure — no side effects export function add(a: number, b: number): number { return a + b; } // Impure — console.log + fetch export async function loadUser(id: string) { console.log('loading', id); return await fetch(`/api/users/${id}`); }
Note: Impurity detection is heuristic-based and conservative — it may flag functions that are effectively pure but use a pattern that looks impure (e.g.
Edit on GitHub
arr.sort() on a locally-created array). Use this as a signal, not a guarantee.