diff --git a/src/browser_action/metric.js b/src/browser_action/metric.js index c961b89..ad5b137 100644 --- a/src/browser_action/metric.js +++ b/src/browser_action/metric.js @@ -1,5 +1,34 @@ import {CLSThresholds, FCPThresholds, FIDThresholds, INPThresholds, LCPThresholds, TTFBThresholds} from './web-vitals.js'; +const assessments = { + 'good': 0, + 'needs-improvement': 1, + 'poor': 2 +}; + +const secondsFormatter = new Intl.NumberFormat(undefined, { + unit: "second", + style: 'unit', + unitDisplay: "short", + minimumFractionDigits: 3, + maximumFractionDigits: 3 +}); + +const millisecondsFormatter = new Intl.NumberFormat(undefined, { + unit: "millisecond", + style: 'unit', + unitDisplay: 'short', + minimumFractionDigits: 0, + maximumFractionDigits: 0 +}); + +const clsFormatter = new Intl.NumberFormat(undefined, { + unitDisplay: 'short', + minimumFractionDigits: 2, + maximumFractionDigits: 2 +}); + + export class Metric { constructor({id, name, local, background, thresholds, rating}) { @@ -9,7 +38,6 @@ export class Metric { this.local = local; this.background = background; this.thresholds = thresholds; - this.digitsOfPrecision = 3; // This will be replaced with field data, if available. this.distribution = [1/3, 1/3, 1/3]; this.rating = rating; @@ -20,11 +48,6 @@ export class Metric { } getAssessmentIndex() { - const assessments = { - 'good': 0, - 'needs-improvement': 1, - 'poor': 2 - }; return assessments[this.rating]; } @@ -67,16 +90,6 @@ export class Metric { return; } - toLocaleFixed({value, unit, precision}) { - return value.toLocaleString(undefined, { - style: unit && 'unit', - unit, - unitDisplay: 'short', - minimumFractionDigits: precision ?? this.digitsOfPrecision, - maximumFractionDigits: precision ?? this.digitsOfPrecision - }); - } - getDensity(i, decimalPlaces=0) { const density = this.distribution[i]; @@ -160,10 +173,7 @@ export class LCP extends Metric { formatValue(value) { value /= 1000; - return this.toLocaleFixed({ - value, - unit: 'second' - }); + return secondsFormatter.format(value); } getInfo() { @@ -199,11 +209,7 @@ export class FID extends Metric { return 'Waiting for input…'; } - return this.toLocaleFixed({ - value, - unit: 'millisecond', - precision: 0 - }); + return millisecondsFormatter.format(value); } } @@ -231,11 +237,7 @@ export class INP extends Metric { return 'Waiting for input…'; } - return this.toLocaleFixed({ - value, - unit: 'millisecond', - precision: 0 - }); + return millisecondsFormatter.format(value); } } @@ -263,10 +265,7 @@ export class CLS extends Metric { } formatValue(value) { - return this.toLocaleFixed({ - value: value, - precision: 2 - }); + return clsFormatter.format(value); } } @@ -295,10 +294,7 @@ export class FCP extends Metric { formatValue(value) { value /= 1000; - return this.toLocaleFixed({ - value, - unit: 'second' - }); + return secondsFormatter.format(value); } getInfo() { @@ -335,10 +331,7 @@ export class TTFB extends Metric { formatValue(value) { value /= 1000; - return this.toLocaleFixed({ - value, - unit: 'second' - }); + return secondsFormatter.format(value); } } diff --git a/src/browser_action/vitals.js b/src/browser_action/vitals.js index 2b7e02c..725785f 100644 --- a/src/browser_action/vitals.js +++ b/src/browser_action/vitals.js @@ -36,9 +36,6 @@ // Identifiable prefix for console logging const LOG_PREFIX = '[Web Vitals Extension]'; - // Default units of precision for HUD - const DEFAULT_UNITS_OF_PRECISION = 3; - // Registry for badge metrics const badgeMetrics = initializeMetrics(); @@ -53,15 +50,27 @@ } }); - function toLocaleFixed({value, unit, precision }) { - return value.toLocaleString(undefined, { - style: unit && 'unit', - unit, - unitDisplay: 'short', - minimumFractionDigits: precision ?? DEFAULT_UNITS_OF_PRECISION, - maximumFractionDigits: precision ?? DEFAULT_UNITS_OF_PRECISION - }); - } + const secondsFormatter = new Intl.NumberFormat(undefined, { + unit: "second", + style: 'unit', + unitDisplay: "short", + minimumFractionDigits: 3, + maximumFractionDigits: 3 + }); + + const millisecondsFormatter = new Intl.NumberFormat(undefined, { + unit: "millisecond", + style: 'unit', + unitDisplay: "short", + minimumFractionDigits: 0, + maximumFractionDigits: 0 + }); + + const clsFormatter = new Intl.NumberFormat(undefined, { + unitDisplay: "short", + minimumFractionDigits: 2, + maximumFractionDigits: 2 + }); function initializeMetrics() { let metricsState = localStorage.getItem('web-vitals-extension-metrics'); @@ -240,15 +249,15 @@ let formattedValue; switch(metric.name) { case 'CLS': - formattedValue = toLocaleFixed({value: metric.value, precision: 2}); + formattedValue = clsFormatter.format(metric.value); break; case 'INP': case 'Interaction': case 'FID': - formattedValue = toLocaleFixed({value: metric.value, unit: 'millisecond', precision: 0}); + formattedValue = millisecondsFormatter.format(metric.value); break; default: - formattedValue = toLocaleFixed({value: metric.value / 1000, unit: 'second', precision: 3}); + formattedValue = secondsFormatter.format(metric.value / 1000); } console.groupCollapsed( `${LOG_PREFIX} ${metric.name} %c${formattedValue} (${metric.rating})`, @@ -521,13 +530,13 @@ Largest Contentful Paint ${tabLoadedInBackground ? 'Value inflated as tab was loaded in background' : ''} -
${toLocaleFixed({value: (metrics.lcp.value || 0)/1000, unit: 'second'})}
+
${secondsFormatter.format((metrics.lcp.value || 0)/1000)}
Cumulative Layout Shift -
${toLocaleFixed({value: metrics.cls.value || 0, precision: 2})}
+
${clsFormatter.format( metrics.cls.value || 0)}
@@ -537,8 +546,7 @@ ${metrics.inp.value === null ? '(waiting for input)' : ''}
${ - metrics.inp.value === null ? '' : - `${toLocaleFixed({value: metrics.inp.value, unit: 'millisecond', precision: 0})}` + metrics.inp.value === null ? '' : `${millisecondsFormatter.format(metrics.inp.value)}` }
@@ -549,8 +557,7 @@ ${metrics.fid.value === null ? '(waiting for input)' : ''}
${ - metrics.fid.value === null ? '' : - `${toLocaleFixed({value: metrics.fid.value, unit: 'millisecond', precision: 0})}` + metrics.fid.value === null ? '' : `${millisecondsFormatter.format(metrics.fid.value)}` }
@@ -560,7 +567,7 @@ First Contentful Paint ${tabLoadedInBackground ? 'Value inflated as tab was loaded in background' : ''} -
${toLocaleFixed({value: (metrics.fcp.value || 0)/1000, unit: 'second'})}
+
${secondsFormatter.format((metrics.fcp.value || 0)/1000)}
@@ -569,7 +576,7 @@ Time to First Byte -
${toLocaleFixed({value: (metrics.ttfb.value || 0)/1000, unit: 'second'})}
+
${secondsFormatter.format((metrics.ttfb.value || 0)/1000)}