formatters.tsx 822 B

12345678910111213141516171819202122232425
  1. const format = (v: number, abbrev: string, precision: number) => {
  2. if (v === 0) {
  3. return '0' + abbrev;
  4. }
  5. return v.toFixed(precision) + abbrev;
  6. };
  7. // We avoid the moment date formatter as it creates a lot of intermediary strings,
  8. // which the trace view is already doing a lot of, so we try to avoid it here as
  9. // gc during scrolling causes jank
  10. export function formatTraceDuration(duration_ms: number) {
  11. if (duration_ms >= 24 * 60 * 60 * 1e3) {
  12. return format((duration_ms / 24) * 60 * 60e3, 'd', 2);
  13. }
  14. if (duration_ms >= 60 * 60 * 1e3) {
  15. return format((duration_ms / 60) * 60e3, 'h', 2);
  16. }
  17. if (duration_ms >= 60 * 1e3) {
  18. return format(duration_ms / 60e3, 'min', 2);
  19. }
  20. if (duration_ms >= 1e3) {
  21. return format(duration_ms / 1e3, 's', 2);
  22. }
  23. return format(duration_ms, 'ms', 2);
  24. }