ms rounding

Benchmark created on


Setup

const HOUR_MS = 60 * 60 * 1000;
const d = new Date("2025-07-02T13:25:30.500Z").getTime();

function m1(timestamp) {
  const startDate = new Date(timestamp);
  startDate.setMinutes(0, 0, 0);
  const startOfHour = startDate.getTime();
  const endDate = new Date(timestamp);
  endDate.setMinutes(59, 59, 999);
  const endOfHour = endDate.getTime();
  return { startOfHour, endOfHour };
}

function m2(timestamp) {
  const startOfHour = Math.floor(timestamp / HOUR_MS) * HOUR_MS;
  const endOfHour = startOfHour + HOUR_MS - 1;
  return { startOfHour, endOfHour };
}

function m3(timestamp) {
  const startOfHour = timestamp - (timestamp % HOUR_MS);
  const endOfHour = startOfHour + HOUR_MS - 1;
  return { startOfHour, endOfHour };
}

Test runner

Ready to run.

Testing in
TestOps/sec
t1
m1(d)
ready
t2
m2(d)
ready
t3
m3(d)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.