parse vtt (v3)

Revision 3 of this 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 hours = parseFloat(time.substring(0, hoursEndIndex));
  const minutes = parseFloat(time.substring(hoursEndIndex + 1, minutesEndIndex));
  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
slow
const cueTimeToSeconds = (timeString) => {
  return timeString
    .split(':')
    .map(parseFloat)
    .reduce((prev, current, index) => {
      return prev + current * [3600, 60, 1][index];
    }, 0);
};
const hour = Math.floor(Math.random()*999);
cueTimeToSeconds(`${hour}:53:26.000`)
ready

Revisions

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