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:

PatternExample
Accesses thisthis.state = …
Calls known impure APIsconsole.log(), fetch(), Math.random(), Date.now(), setTimeout()
Mutates a parameter propertyparam.x = 1
Writes to outer-scope variablesouterVar = …
Uses awaitawait fetch(…)
Calls array mutation methodsarr.push(), arr.splice(), arr.sort()
Uses deletedelete 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. arr.sort() on a locally-created array). Use this as a signal, not a guarantee.
Edit on GitHub