parse vtt

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Split
const cueTimeToSecondsNew = (time) => {
  // Assume: "HH:MM:SS.sss" or "H:MM:SS.sss" (hours can be more than two digits)
  const parts = time.split(':');
  const hours = parseInt(parts[0], 10);
  const minutes = parseInt(parts[1], 10);
  const seconds = parseFloat(parts[2]);

  return hours * 3600 + minutes * 60 + seconds;
};
const hour = Math.floor(Math.random()*999);
cueTimeToSecondsNew(`${hour}:53:26.000`)
ready
substring
const cueTimeToSecondsNew = (time) => {
  // Assume: "HHHH:mm:ss.sss" or "H:MM:SS.sss" (hours can be more than two digits)
  const hoursEndIndex = time.indexOf(':');
  const minutesEndIndex = time.indexOf(':', hoursEndIndex + 1);
  const secondsEndIndex = time.indexOf('.', minutesEndIndex + 1);

  const hours = parseInt(time.substring(0, hoursEndIndex), 10);
  const minutes = parseInt(time.substring(hoursEndIndex + 1, minutesEndIndex), 10);
  const seconds = parseFloat(time.substring(minutesEndIndex + 1));

  return hours * 3600 + minutes * 60 + seconds;
};
const hour = Math.floor(Math.random()*999);
cueTimeToSecondsNew(`${hour}:53:26.000`)
ready
oldest
const cueTimeToSecondsNewOld = (timeString) => {
   const timePattern = /^(\d{2}):(\d{2}):(\d{2}\.\d+)$/;
   const match = timeString.match(timePattern);

   if (!match) {
     throw new Error(`Invalid time format: ${timeString}`);
   }

   const [, hours, minutes, seconds] = match;
   return parseInt(hours, 10) * 3600 + parseInt(minutes, 10) * 60 + parseFloat(seconds);
};

const hour = Math.floor(Math.random()*99);
cueTimeToSecondsNewOld(`${hour}:53:26.000`)
ready

Revisions

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