# misc/frameprobe (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/misc/frameprobe/

Detects slow devices. (measures frame intervals for the first few seconds, used to automatically turn on a lightweight path that disables heavy effects)

## FrameProbe

Detects slow devices. (measures frame intervals for the first few seconds, used to automatically turn on a lightweight path that disables heavy effects)
- After measuring starts and the wait seconds pass, looks at each frame's interval during the measure seconds. (the first few seconds stutter while models and images load)
- If frames exceeding the slow multiple of the target interval (one frame at the max frame rate, or one frame at 60 if unlimited) make up at least the slow fraction, it is a slow device.
  - Why 1.4 times rather than one frame: a 90 Hz screen with max 60 has the engine draw every other refresh, so the interval is always 22 ms, and that is not the device being slow.
  - On 60 Hz and 120 Hz screens, a frame that missed one refresh (33 ms, 25 ms) counts as slow. One in four means below 48 fps.
- Once judged a slow device, it does not measure again. If the window is hidden before measuring finishes, restart() measures again from the start.
  If not slow, measureAgain() can measure once more in a heavy scene.
- Does not import the engine or the browser. The caller passes the frame interval and max frame rate.

Example: Detect a slow device by feeding frame intervals

```js
import { FrameProbe } from "./build/vanilla.js";

const frameProbe = new FrameProbe();
frameProbe.setSeconds(0.5, 1);
let isFinished = false;
for (let frame = 0; frame < 120 && !isFinished; ++frame) {
	const frameSeconds = frame % 3 === 0 ? 1 / 30 : 1 / 60;
	isFinished = frameProbe.measureFrame(frameSeconds, 60);
}
console.log(frameProbe.isFinished(), frameProbe.isSlow(), frameProbe.readSlowFrameShare());
```

- `constructor()` (constructor, since 0.4.0): Constructor.
- `measureFrame(frameSeconds, maximumFramePerSecond)` (method, since 0.4.0): Measures one frame. (returns true on the frame where measuring finishes)
  - `frameSeconds` number: Interval since the last frame. (seconds)
  - `maximumFramePerSecond` number: Max frame rate. (0 means unlimited)
  - returns boolean: True if measuring finished on this frame. (false if already finished or the interval is 0 or less)
- `restart()` (method, since 0.4.0): Measures again from the start. (when the window was hidden before measuring finished, left as is if already finished)
- `measureAgain()` (method, since 0.4.0): Reopens a finished measurement once more. (only when not slow, from the wait seconds as at first)
  - Used to measure again in a heavy scene after the light scene at startup.
- `isFinished()` (method, since 0.4.0): Returns whether measuring is finished.
  - returns boolean: True if measuring has finished.
- `isSlow()` (method, since 0.4.0): Returns whether it is a slow device. (false before measuring finishes)
  - returns boolean: True if judged to be a slow device.
- `readSlowFrameShare()` (method, since 0.4.0): Returns the slow frame fraction so far. (0 if no frames were measured)
  - returns number: Slow frame count divided by the measured frame count. (0 ~ 1)
- `setSeconds(warmupSeconds, probeSeconds)` (method, since 0.4.0): Sets the wait seconds and measure seconds. (set before measuring)
  - `warmupSeconds` number: Seconds to wait before measuring. (0 if negative, 2 at first)
  - `probeSeconds` number: Seconds to measure. (0 if negative, 4 at first)
- `getWarmupSeconds()` (method, since 0.4.0): Returns the wait seconds.
  - returns number: Seconds to wait before measuring.
- `getProbeSeconds()` (method, since 0.4.0): Returns the measure seconds.
  - returns number: Seconds to measure.
- `setSlowFrameThreshold(slowFrameRatio, slowFrameShare)` (method, since 0.4.0): Sets the slow frame criteria. (multiple of the target interval, fraction to count as a slow device)
  - `slowFrameRatio` number: Interval counted as a slow frame. (a multiple of the target interval, 1.4 at first)
  - `slowFrameShare` number: Share of slow frames that marks a slow device. (0 ~ 1, 0.25 at first)
- `getSlowFrameRatio()` (method, since 0.4.0): Returns the interval multiple counted as a slow frame.
  - returns number: Multiple of the interval counted as a slow frame.
- `getSlowFrameShare()` (method, since 0.4.0): Returns the fraction of slow frames to count as a slow device.
  - returns number: Share of slow frames that marks a slow device.
- `setUnlimitedTargetFramePerSecond(unlimitedTargetFramePerSecond)` (method, since 0.4.0): Sets the target when the max frame rate is unlimited.
  - `unlimitedTargetFramePerSecond` number: Frames per second used as the target when the maximum frame rate is 0 (unlimited). (60 at first)
- `getUnlimitedTargetFramePerSecond()` (method, since 0.4.0): Returns the target when the max frame rate is unlimited.
  - returns number: Target frames per second when the maximum frame rate is unlimited.
