formatters.tsx 836 B

12345678910111213141516171819202122232425262728
  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 <= 0) {
  12. return '0ms';
  13. }
  14. if (duration_ms < 1000) {
  15. return format(duration_ms, 'ms', 2);
  16. }
  17. if (duration_ms < 60000) {
  18. return format(duration_ms / 1000, 's', 2);
  19. }
  20. if (duration_ms < 3600000) {
  21. return format(duration_ms / 60000, 'm', 2);
  22. }
  23. if (duration_ms < 86400000) {
  24. return format(duration_ms / 3600000, 'h', 2);
  25. }
  26. return format(duration_ms / 86400000, 'd', 2);
  27. }