Fork me on GitHub

TomasMikula/blog

Measuring FPS with ReactFX

I came across this StackOverflow question about measuring frame rate in JavaFX and while James_D gave a nice solution, I still found it a little too verbose for such a simple task.

So I just added one more feature before releasing ReactFX 2.0 Milestone 2, namely animationTicks(). Using that, the solution now looks like this:

EventStreams.animationTicks()
        .latestN(100)
        .map(ticks -> {
            int n = ticks.size() - 1;
            return n * 1_000_000_000.0 / (ticks.get(n) - ticks.get(0));
        })
        .map(d -> String.format("FPS: %.3f", d))
        .feedTo(label.textProperty());

Here is the full runnable demo, adapted from James’ answer.

comments powered by Disqus