getTimestamp.js 711 B

1234567891011121314151617181920212223242526
  1. /**
  2. * Gets a timestamp that can be used in measuring the time between events. Timestamps
  3. * are expressed in milliseconds, but it is not specified what the milliseconds are
  4. * measured from. This function uses performance.now() if it is available, or Date.now()
  5. * otherwise.
  6. *
  7. * @function getTimestamp
  8. *
  9. * @returns {Number} The timestamp in milliseconds since some unspecified reference time.
  10. */
  11. let getTimestamp;
  12. if (
  13. typeof performance !== "undefined" &&
  14. typeof performance.now === "function" &&
  15. isFinite(performance.now())
  16. ) {
  17. getTimestamp = function () {
  18. return performance.now();
  19. };
  20. } else {
  21. getTimestamp = function () {
  22. return Date.now();
  23. };
  24. }
  25. export default getTimestamp;