DEV Community

Build A Ring Farm Guide
Build A Ring Farm Guide

Posted on Fully Autonomous

Checking CPS Arithmetic in JavaScript: Durations, Weighted Rates, and Repeatable Inputs

A clicks-per-second (CPS) counter looks like a tiny calculator: divide a count by a duration. The interesting bugs appear around that division—invalid inputs, inconsistent timing, rounding, and comparisons that silently change their denominator.

Here is a small calculation layer you can test separately from a browser's event handlers. It is an illustrative implementation, not the source code of a particular website.

Keep validation separate from presentation

function calculateCps(clicks, durationSeconds) {
  if (!Number.isSafeInteger(clicks) || clicks < 0) {
    throw new RangeError("clicks must be a non-negative safe integer");
  }
  if (!Number.isFinite(durationSeconds) || durationSeconds <= 0) {
    throw new RangeError("durationSeconds must be a positive finite number");
  }

  const rate = clicks / durationSeconds;
  if (!Number.isFinite(rate)) {
    throw new RangeError("calculated rate is outside the finite range");
  }
  return rate;
}

const score = calculateCps(40, 5);
console.log(score);            // 8
console.log(score.toFixed(2)); // "8.00" — display text only
Enter fullscreen mode Exit fullscreen mode

Keep the unrounded number for subsequent calculations. Formatting the result early can turn it into a string and lose precision. This function also deliberately rejects strings such as "5"; parse form inputs at the UI boundary instead of relying on implicit conversion inside the calculation.

MDN's documentation for Number.isFinite explains why the number-specific check is useful here: it rejects non-number values rather than coercing them.

Useful cases to verify are zero clicks, a fractional positive duration, negative clicks, fractional clicks, zero duration, NaN, and Infinity. The first two can be valid; the remaining examples should be rejected by this implementation.

Decide what the duration means

A configured five-second window and the actual elapsed time until a delayed callback runs are different quantities. Define the counting window explicitly: what starts it, what closes it, and whether a click at the closing boundary is included. For a scheduled duration, count only events within that defined window and divide by that duration. For an elapsed-time measurement, divide by the measured interval and label it accordingly.

A background tab, a busy main thread, or a delayed UI update can complicate the event lifecycle. Testing the arithmetic alone cannot verify that lifecycle. Exercise the start, finish, reset, and restart behavior separately; make sure a previous run cannot add clicks to a later one.

An average of rates is not always a combined rate

Consider two illustrative records:

Record Clicks Seconds CPS
A 40 5 8
B 60 10 6

The arithmetic mean of the two displayed rates is 7 CPS. But the rate across the combined recorded time is:

const combinedRate = (40 + 60) / (5 + 10);
console.log(combinedRate.toFixed(2)); // "6.67"
Enter fullscreen mode Exit fullscreen mode

These answer different questions. If durations are equal, an ordinary mean of the rates matches the combined rate. If durations differ, weight by duration when calculating a combined rate.

That mathematical aggregation does not make runs with different durations interchangeable as a human benchmark. A short burst and a longer run can involve different pacing and fatigue.

Record the conditions as well as the score

For a browser exercise, a record can include the duration, click count, input method, device, browser, and whether the run was interrupted. Compare repeated attempts under the same conditions. Label scripted clicks as software tests; they do not measure a person's clicking speed.

A hands-on tool for exploring timed clicking is CPS-TEST.CO. Try the same duration and input device for several runs, then check the arithmetic against your recorded click counts. A CPS result describes that timed input task; it does not establish reaction time or overall gaming ability.

Disclosure: this article was prepared with AI assistance for the owner of CPS-TEST.CO and includes a link to that website. The numerical records above are examples, not claimed measurement results.

Top comments (0)