{"id":10950,"date":"2023-08-27T16:34:24","date_gmt":"2023-08-27T08:34:24","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10950"},"modified":"2023-08-27T22:50:57","modified_gmt":"2023-08-27T14:50:57","slug":"js-performanceobserver-api","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2023\/08\/js-performanceobserver-api\/","title":{"rendered":"\u72e0\u72e0\u5730\u7814\u7a76\u4e86\u4e0b PerformanceObserver API"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10950\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=10950<\/a> \u946b\u7a7a\u95f4-\u946b\u751f\u6d3b<br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202308-performance.png';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202308\/performance-obverse-cover.png\" width=\"435\" height=\"277\" alt=\"\u4e4c\u9e26\u5357\u74dc\u5c01\u9762\u56fe\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001Observer\u5bb6\u65cf<\/h3>\n<p>\u6ca1\u60f3\u5230Observer\u5bb6\u65cf\u8fd8\u6709\u4e2aPerformanceObserver\uff0c\u8fd9\u5c31\u6709\u70b9\u532a\u5937\u6240\u601d\uff0c\u5c31\u662f\uff0c\u4e4b\u524d\u6211\u660e\u660e\u597d\u597d\u68b3\u7406\u8fc7\u6d4f\u89c8\u5668\u76ee\u524d\u652f\u6301\u7684Observer\uff0c\u4f46\u662f\u90fd2023\u5e74\u4e86\uff0c\u7a81\u7136\u51fa\u73b0\u4e2aPerformanceObserver\uff0c\u5c31\u975e\u5e38\u8ba9\u4eba\u89c9\u5f97\u4e0d\u771f\u5b9e\uff0c\u4e4b\u524d\u600e\u4e48\u5c31\u6ca1\u770b\u5230\u5462\uff1f<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202308\/2023-08-15_222813.jpg\" width=\"96\" height=\"82\" alt=\"\u532a\u5937\u6240\u601d\" class=\"alignnone size-medium\" \/><\/p>\n<p>MutationObserver\u53ef\u4ee5\u7528\u6765<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2019\/08\/js-dom-mutation-observer\/\">\u89c2\u5bdfDOM\u5143\u7d20\u7684\u53d8\u5316<\/a>\uff0cResizeObserver\u53ef\u4ee5\u7528\u6765\u89c2\u5bdf<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2020\/03\/dom-resize-api-resizeobserver\/\">DOM\u5143\u7d20\u5c3a\u5bf8\u7684\u53d8\u5316<\/a>\uff0cIntersectionObserver\u5219\u53ef\u4ee5<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2020\/12\/js-intersectionobserver-nav\/\">\u89c2\u5bdf\u5143\u7d20\u548c\u6d4f\u89c8\u5668\u89c6\u7a97\u76f8\u4ea4<\/a>\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u800c\u672c\u6587\u8981\u4ecb\u7ecd\u7684 PerformanceObserver \u5219\u53ef\u4ee5\u5bf9\u9875\u9762\u7684\u5404\u9879\u6027\u80fd\u6307\u6807\u8fdb\u884c\u89c2\u5bdf\u3002<\/p>\n<h3>\u4e8c\u3001\u5173\u4e8e\u6027\u80fd Performance API<\/h3>\n<p>Performance API\u662f\u4e2a\u5e9e\u5927\u7684\u5bb6\u65cf\uff0c\u6839\u672c\u5b66\u4e0d\u52a8\u7684\uff0c\u770b\u770b\u4e0b\u9762\u8fd9\u4e9b\uff08\u53c2\u8003\u81ea<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Performance_API\" rel=\"noopener\" target=\"_blank\">MDN\u6587\u6863<\/a>\uff09\uff0c\u77e5\u8bc6\u5bc6\u5ea6\u4e5f\u4e0d\u662f\u4e00\u822c\u7684\u9ad8\u3002<\/p>\n<p><svg width=\"661\" height=\"830\" viewBox=\"-0.5 -0.5 661 830\" xmlns:xlink=\"http:\/\/www.w3.org\/1999\/xlink\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" xmlns:xhtml=\"http:\/\/www.w3.org\/1999\/xhtml\"><path fill=\"#fff\" stroke=\"#000\" pointer-events=\"all\" d=\"M125 85h180v40H125z\"\/><switch transform=\"translate(-.5 -.5)\"><foreignObject pointer-events=\"none\" width=\"100%\" height=\"100%\" requiredFeatures=\"http:\/\/www.w3.org\/TR\/SVG11\/feature#Extensibility\" style=\"overflow:visible;text-align:left\"><xhtml:div style=\"display:flex;align-items:unsafe center;justify-content:unsafe center;width:178px;height:1px;padding-top:105px;margin-left:126px\"><xhtml:div data-drawio-colors=\"color: rgb(0, 0, 0);\" style=\"box-sizing:border-box;font-size:0;text-align:center\"><xhtml:div style=\"display:inline-block;font-size:12px;font-family:Helvetica;color:#000;line-height:1.2;pointer-events:all;white-space:normal;overflow-wrap:normal\"><xhtml:a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/PerformanceEntry\">PerformanceEntry<\/xhtml:a><\/xhtml:div><\/xhtml:div><\/xhtml:div><\/foreignObject><text x=\"215\" y=\"109\" font-family=\"Helvetica\" font-size=\"12\" text-anchor=\"middle\">PerformanceEntry<\/text><\/switch><path fill=\"#fff\" stroke=\"#000\" pointer-events=\"all\" d=\"M235 465h180v40H235z\"\/><switch transform=\"translate(-.5 -.5)\"><foreignObject pointer-events=\"none\" width=\"100%\" height=\"100%\" requiredFeatures=\"http:\/\/www.w3.org\/TR\/SVG11\/feature#Extensibility\" style=\"overflow:visible;text-align:left\"><xhtml:div style=\"display:flex;align-items:unsafe center;justify-content:unsafe center;width:178px;height:1px;padding-top:485px;margin-left:236px\"><xhtml:div data-drawio-colors=\"color: rgb(0, 0, 0);\" style=\"box-sizing:border-box;font-size:0;text-align:center\"><xhtml:div style=\"display:inline-block;font-size:12px;font-family:Helvetica;color:#000;line-height:1.2;pointer-events:all;white-space:normal;overflow-wrap:normal\"><xhtml:a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/PerformanceMark\">PerformanceMark<\/xhtml:a><\/xhtml:div><\/xhtml:div><\/xhtml:div><\/foreignObject><text x=\"325\" y=\"489\" font-family=\"Helvetica\" font-size=\"12\" text-anchor=\"middle\">PerformanceMark<\/text><\/switch><path fill=\"#fff\" stroke=\"#000\" pointer-events=\"all\" d=\"M235 525h180v40H235z\"\/><switch transform=\"translate(-.5 -.5)\"><foreignObject pointer-events=\"none\" width=\"100%\" height=\"100%\" requiredFeatures=\"http:\/\/www.w3.org\/TR\/SVG11\/feature#Extensibility\" style=\"overflow:visible;text-align:left\"><xhtml:div style=\"display:flex;align-items:unsafe center;justify-content:unsafe center;width:178px;height:1px;padding-top:545px;margin-left:236px\"><xhtml:div data-drawio-colors=\"color: rgb(0, 0, 0);\" style=\"box-sizing:border-box;font-size:0;text-align:center\"><xhtml:div style=\"display:inline-block;font-size:12px;font-family:Helvetica;color:#000;line-height:1.2;pointer-events:all;white-space:normal;overflow-wrap:normal\"><xhtml:a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/PerformanceMeasure\">PerformanceMeasure<\/xhtml:a><\/xhtml:div><\/xhtml:div><\/xhtml:div><\/foreignObject><text x=\"325\" y=\"549\" font-family=\"Helvetica\" font-size=\"12\" text-anchor=\"middle\">PerformanceMeasure<\/text><\/switch><path fill=\"#fff\" stroke=\"#000\" pointer-events=\"all\" d=\"M235 585h180v40H235z\"\/><switch transform=\"translate(-.5 -.5)\"><foreignObject pointer-events=\"none\" width=\"100%\" height=\"100%\" requiredFeatures=\"http:\/\/www.w3.org\/TR\/SVG11\/feature#Extensibility\" style=\"overflow:visible;text-align:left\"><xhtml:div style=\"display:flex;align-items:unsafe center;justify-content:unsafe center;width:178px;height:1px;padding-top:605px;margin-left:236px\"><xhtml:div data-drawio-colors=\"color: rgb(0, 0, 0);\" style=\"box-sizing:border-box;font-size:0;text-align:center\"><xhtml:div style=\"display:inline-block;font-size:12px;font-family:Helvetica;color:#000;line-height:1.2;pointer-events:all;white-space:normal;overflow-wrap:normal\"><xhtml:a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/PerformancePaintTiming\">PerformancePaintTiming<\/xhtml:a><\/xhtml:div><\/xhtml:div><\/xhtml:div><\/foreignObject><text x=\"325\" y=\"609\" font-family=\"Helvetica\" font-size=\"12\" text-anchor=\"middle\">PerformancePaintTiming<\/text><\/switch><path fill=\"#fff\" stroke=\"#000\" pointer-events=\"all\" d=\"M235 645h180v40H235z\"\/><switch transform=\"translate(-.5 -.5)\"><foreignObject pointer-events=\"none\" width=\"100%\" height=\"100%\" requiredFeatures=\"http:\/\/www.w3.org\/TR\/SVG11\/feature#Extensibility\" style=\"overflow:visible;text-align:left\"><xhtml:div style=\"display:flex;align-items:unsafe center;justify-content:unsafe center;width:178px;height:1px;padding-top:665px;margin-left:236px\"><xhtml:div data-drawio-colors=\"color: rgb(0, 0, 0);\" style=\"box-sizing:border-box;font-size:0;text-align:center\"><xhtml:div style=\"display:inline-block;font-size:12px;font-family:Helvetica;color:#000;line-height:1.2;pointer-events:all;white-space:normal;overflow-wrap:normal\"><xhtml:a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/PerformanceResourceTiming\">PerformanceResourceTiming<\/xhtml:a><\/xhtml:div><\/xhtml:div><\/xhtml:div><\/foreignObject><text x=\"325\" y=\"669\" font-family=\"Helvetica\" font-size=\"12\" text-anchor=\"middle\">PerformanceResourceTiming<\/text><\/switch><path fill=\"#fff\" stroke=\"#000\" pointer-events=\"all\" d=\"M455 645h180v40H455z\"\/><switch transform=\"translate(-.5 -.5)\"><foreignObject pointer-events=\"none\" width=\"100%\" height=\"100%\" requiredFeatures=\"http:\/\/www.w3.org\/TR\/SVG11\/feature#Extensibility\" style=\"overflow:visible;text-align:left\"><xhtml:div style=\"display:flex;align-items:unsafe center;justify-content:unsafe center;width:178px;height:1px;padding-top:665px;margin-left:456px\"><xhtml:div data-drawio-colors=\"color: rgb(0, 0, 0);\" style=\"box-sizing:border-box;font-size:0;text-align:center\"><xhtml:div style=\"display:inline-block;font-size:12px;font-family:Helvetica;color:#000;line-height:1.2;pointer-events:all;white-space:normal;overflow-wrap:normal\"><xhtml:a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/PerformanceNavigationTiming\">PerformanceNavigationTiming<\/xhtml:a><\/xhtml:div><\/xhtml:div><\/xhtml:div><\/foreignObject><text x=\"545\" y=\"669\" font-family=\"Helvetica\" font-size=\"12\" text-anchor=\"middle\">PerformanceNavigationTiming<\/text><\/switch><path fill=\"#fff\" stroke=\"#000\" pointer-events=\"all\" d=\"M235 285h180v40H235z\"\/><switch transform=\"translate(-.5 -.5)\"><foreignObject pointer-events=\"none\" width=\"100%\" height=\"100%\" requiredFeatures=\"http:\/\/www.w3.org\/TR\/SVG11\/feature#Extensibility\" style=\"overflow:visible;text-align:left\"><xhtml:div style=\"display:flex;align-items:unsafe center;justify-content:unsafe center;width:178px;height:1px;padding-top:305px;margin-left:236px\"><xhtml:div data-drawio-colors=\"color: rgb(0, 0, 0);\" style=\"box-sizing:border-box;font-size:0;text-align:center\"><xhtml:div style=\"display:inline-block;font-size:12px;font-family:Helvetica;color:#000;line-height:1.2;pointer-events:all;white-space:normal;overflow-wrap:normal\"><xhtml:a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/PerformanceElementTiming\">PerformanceElementTiming<\/xhtml:a><\/xhtml:div><\/xhtml:div><\/xhtml:div><\/foreignObject><text x=\"325\" y=\"309\" font-family=\"Helvetica\" font-size=\"12\" text-anchor=\"middle\">PerformanceElementTiming<\/text><\/switch><path fill=\"#fff\" stroke=\"#000\" pointer-events=\"all\" d=\"M235 405h180v40H235z\"\/><switch transform=\"translate(-.5 -.5)\"><foreignObject pointer-events=\"none\" width=\"100%\" height=\"100%\" requiredFeatures=\"http:\/\/www.w3.org\/TR\/SVG11\/feature#Extensibility\" style=\"overflow:visible;text-align:left\"><xhtml:div style=\"display:flex;align-items:unsafe center;justify-content:unsafe center;width:178px;height:1px;padding-top:425px;margin-left:236px\"><xhtml:div data-drawio-colors=\"color: rgb(0, 0, 0);\" style=\"box-sizing:border-box;font-size:0;text-align:center\"><xhtml:div style=\"display:inline-block;font-size:12px;font-family:Helvetica;color:#000;line-height:1.2;pointer-events:all;white-space:normal;overflow-wrap:normal\"><xhtml:a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/PerformanceLongTaskTiming\">PerformanceLongTaskTiming<\/xhtml:a><\/xhtml:div><\/xhtml:div><\/xhtml:div><\/foreignObject><text x=\"325\" y=\"429\" font-family=\"Helvetica\" font-size=\"12\" text-anchor=\"middle\">PerformanceLongTaskTiming<\/text><\/switch><path fill=\"#fff\" stroke=\"#000\" pointer-events=\"all\" d=\"M235 345h180v40H235z\"\/><switch transform=\"translate(-.5 -.5)\"><foreignObject pointer-events=\"none\" width=\"100%\" height=\"100%\" requiredFeatures=\"http:\/\/www.w3.org\/TR\/SVG11\/feature#Extensibility\" style=\"overflow:visible;text-align:left\"><xhtml:div style=\"display:flex;align-items:unsafe center;justify-content:unsafe center;width:178px;height:1px;padding-top:365px;margin-left:236px\"><xhtml:div data-drawio-colors=\"color: rgb(0, 0, 0);\" style=\"box-sizing:border-box;font-size:0;text-align:center\"><xhtml:div style=\"display:inline-block;font-size:12px;font-family:Helvetica;color:#000;line-height:1.2;pointer-events:all;white-space:normal;overflow-wrap:normal\"><xhtml:a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/PerformanceEventTiming\">PerformanceEventTiming<\/xhtml:a><\/xhtml:div><\/xhtml:div><\/xhtml:div><\/foreignObject><text x=\"325\" y=\"369\" font-family=\"Helvetica\" font-size=\"12\" text-anchor=\"middle\">PerformanceEventTiming<\/text><\/switch><path fill=\"#fff\" stroke=\"#000\" pointer-events=\"all\" d=\"M235 705h180v40H235z\"\/><switch transform=\"translate(-.5 -.5)\"><foreignObject pointer-events=\"none\" width=\"100%\" height=\"100%\" requiredFeatures=\"http:\/\/www.w3.org\/TR\/SVG11\/feature#Extensibility\" style=\"overflow:visible;text-align:left\"><xhtml:div style=\"display:flex;align-items:unsafe center;justify-content:unsafe center;width:178px;height:1px;padding-top:725px;margin-left:236px\"><xhtml:div data-drawio-colors=\"color: rgb(0, 0, 0);\" style=\"box-sizing:border-box;font-size:0;text-align:center\"><xhtml:div style=\"display:inline-block;font-size:12px;font-family:Helvetica;color:#000;line-height:1.2;pointer-events:all;white-space:normal;overflow-wrap:normal\"><xhtml:a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/TaskAttribution\">TaskAttributionTiming<\/xhtml:a><\/xhtml:div><\/xhtml:div><\/xhtml:div><\/foreignObject><text x=\"325\" y=\"729\" font-family=\"Helvetica\" font-size=\"12\" text-anchor=\"middle\">TaskAttributionTiming<\/text><\/switch><path fill=\"#fff\" stroke=\"#000\" pointer-events=\"all\" d=\"M235 165h180v40H235z\"\/><switch transform=\"translate(-.5 -.5)\"><foreignObject pointer-events=\"none\" width=\"100%\" height=\"100%\" requiredFeatures=\"http:\/\/www.w3.org\/TR\/SVG11\/feature#Extensibility\" style=\"overflow:visible;text-align:left\"><xhtml:div style=\"display:flex;align-items:unsafe center;justify-content:unsafe center;width:178px;height:1px;padding-top:185px;margin-left:236px\"><xhtml:div data-drawio-colors=\"color: rgb(0, 0, 0);\" style=\"box-sizing:border-box;font-size:0;text-align:center\"><xhtml:div style=\"display:inline-block;font-size:12px;font-family:Helvetica;color:#000;line-height:1.2;pointer-events:all;white-space:normal;overflow-wrap:normal\"><xhtml:a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/PerformanceEventTiming\">LargestContentfulPaint<\/xhtml:a><\/xhtml:div><\/xhtml:div><\/xhtml:div><\/foreignObject><text x=\"325\" y=\"189\" font-family=\"Helvetica\" font-size=\"12\" text-anchor=\"middle\">LargestContentfulPaint<\/text><\/switch><path d=\"M215 785.356V131.37\" fill=\"none\" stroke=\"#000\" stroke-miterlimit=\"10\" pointer-events=\"stroke\" style=\"stroke-width:1.04961\"\/><path d=\"m215 126.12 3.5 7-3.5-1.75-3.5 1.75z\" stroke=\"#000\" stroke-miterlimit=\"10\" pointer-events=\"all\"\/><path d=\"M215 305h20m-20-120h20m-20 180h20m-20 60h20m-20 60h20m-20 60h20m-20 60h20m-20 60h20m-20 60h20m220-60h-33.63\" fill=\"none\" stroke=\"#000\" stroke-miterlimit=\"10\" pointer-events=\"stroke\"\/><path d=\"m416.12 665 7-3.5-1.75 3.5 1.75 3.5z\" stroke=\"#000\" stroke-miterlimit=\"10\" pointer-events=\"all\"\/><path fill=\"#fff\" stroke=\"#000\" pointer-events=\"all\" d=\"M25 25h180v40H25z\"\/><switch transform=\"translate(-.5 -.5)\"><foreignObject pointer-events=\"none\" width=\"100%\" height=\"100%\" requiredFeatures=\"http:\/\/www.w3.org\/TR\/SVG11\/feature#Extensibility\" style=\"overflow:visible;text-align:left\"><xhtml:div style=\"display:flex;align-items:unsafe center;justify-content:unsafe center;width:178px;height:1px;padding-top:45px;margin-left:26px\"><xhtml:div data-drawio-colors=\"color: rgb(0, 0, 0);\" style=\"box-sizing:border-box;font-size:0;text-align:center\"><xhtml:div style=\"display:inline-block;font-size:12px;font-family:Helvetica;color:#000;line-height:1.2;pointer-events:all;white-space:normal;overflow-wrap:normal\"><xhtml:a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Performance\">Performance<\/xhtml:a><\/xhtml:div><\/xhtml:div><\/xhtml:div><\/foreignObject><text x=\"115\" y=\"49\" font-family=\"Helvetica\" font-size=\"12\" text-anchor=\"middle\">Performance<\/text><\/switch><path fill=\"#fff\" stroke=\"#000\" pointer-events=\"all\" d=\"M225 25h180v40H225z\"\/><switch transform=\"translate(-.5 -.5)\"><foreignObject pointer-events=\"none\" width=\"100%\" height=\"100%\" requiredFeatures=\"http:\/\/www.w3.org\/TR\/SVG11\/feature#Extensibility\" style=\"overflow:visible;text-align:left\"><xhtml:div style=\"display:flex;align-items:unsafe center;justify-content:unsafe center;width:178px;height:1px;padding-top:45px;margin-left:226px\"><xhtml:div data-drawio-colors=\"color: rgb(0, 0, 0);\" style=\"box-sizing:border-box;font-size:0;text-align:center\"><xhtml:div style=\"display:inline-block;font-size:12px;font-family:Helvetica;color:#000;line-height:1.2;pointer-events:all;white-space:normal;overflow-wrap:normal\"><xhtml:a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/PerformanceObserver\">PerformanceObserver<\/xhtml:a><\/xhtml:div><\/xhtml:div><\/xhtml:div><\/foreignObject><text x=\"315\" y=\"49\" font-family=\"Helvetica\" font-size=\"12\" text-anchor=\"middle\">PerformanceObserver<\/text><\/switch><path fill=\"#fff\" stroke=\"#000\" pointer-events=\"all\" d=\"M235 225h180v40H235z\"\/><switch transform=\"translate(-.5 -.5)\"><foreignObject pointer-events=\"none\" width=\"100%\" height=\"100%\" requiredFeatures=\"http:\/\/www.w3.org\/TR\/SVG11\/feature#Extensibility\" style=\"overflow:visible;text-align:left\"><xhtml:div style=\"display:flex;align-items:unsafe center;justify-content:unsafe center;width:178px;height:1px;padding-top:245px;margin-left:236px\"><xhtml:div data-drawio-colors=\"color: rgb(0, 0, 0);\" style=\"box-sizing:border-box;font-size:0;text-align:center\"><xhtml:div style=\"display:inline-block;font-size:12px;font-family:Helvetica;color:#000;line-height:1.2;pointer-events:all;white-space:normal;overflow-wrap:normal\"><xhtml:a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/LayoutShift\">LayoutShift<\/xhtml:a><\/xhtml:div><\/xhtml:div><\/xhtml:div><\/foreignObject><text x=\"325\" y=\"249\" font-family=\"Helvetica\" font-size=\"12\" text-anchor=\"middle\">LayoutShift<\/text><\/switch><path d=\"M215 245h20\" fill=\"none\" stroke=\"#000\" stroke-miterlimit=\"10\" pointer-events=\"stroke\"\/><path fill=\"#fff\" stroke=\"#000\" pointer-events=\"all\" d=\"M235 764.383h180v40H235z\"\/><switch transform=\"translate(-1.056 57.535)\"><foreignObject pointer-events=\"none\" width=\"100%\" height=\"100%\" requiredFeatures=\"http:\/\/www.w3.org\/TR\/SVG11\/feature#Extensibility\" style=\"overflow:visible;text-align:left\"><xhtml:div style=\"display:flex;align-items:unsafe center;justify-content:unsafe center;width:178px;height:1px;padding-top:725px;margin-left:236px\"><xhtml:div data-drawio-colors=\"color: rgb(0, 0, 0);\" style=\"box-sizing:border-box;font-size:0;text-align:center\"><xhtml:div style=\"display:inline-block;font-size:12px;font-family:Helvetica;color:#000;line-height:1.2;pointer-events:all;white-space:normal;overflow-wrap:normal\"><xhtml:a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/VisibilityStateEntry\">VisibilityStateEntry<\/xhtml:a><\/xhtml:div><\/xhtml:div><\/xhtml:div><\/foreignObject><text x=\"325.973\" y=\"729.974\" font-family=\"Helvetica\" font-size=\"12\" text-anchor=\"middle\">VisibilityStateEntry<\/text><\/switch><path d=\"M214.743 784.849h20\" fill=\"none\" stroke=\"#000\" stroke-miterlimit=\"10\" pointer-events=\"stroke\"\/><\/svg><\/p>\n<p>PerformanceObserver\u662f\u5176\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u90e8\u5206\u5185\u5bb9\u3002<\/p>\n<h3>\u4e09\u3001\u5305\u542b\u7684\u6027\u80fd\u6307\u6807<\/h3>\n<p>PerformanceObserver\u4e2d\u5305\u542b\u5982\u4e0b\u8868\u6240\u793a\u7684\u4e00\u7cfb\u5217\u6027\u80fd\u6307\u6807\uff08\u53ef\u4ee5\u901a\u8fc7 PerformanceObserver.supportedEntryTypes \u8fd4\u56de\uff0c\u4e0d\u540c\u6d4f\u89c8\u5668\u7684\u652f\u6301\u60c5\u51b5\u53ef\u80fd\u4f1a\u6709\u5dee\u5f02\uff09\uff1a<\/p>\n<table class=\"params_table\" cellspacing=\"1\">\n<thead>\n<tr>\n<th align=\"center\">\u6307\u6807<\/th>\n<th align=\"center\">\u91ca\u4e49<\/th>\n<th align=\"center\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/study\/browser\/chrome.png\" width=\"48\" height=\"48\" alt=\"Chrome logo\"><\/th>\n<th align=\"center\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/study\/browser\/firefox.png\" width=\"48\" height=\"48\" alt=\"Firefox logo\"><\/th>\n<th align=\"center\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/study\/browser\/safari.png\" width=\"48\" height=\"48\" alt=\"Safari logo\"><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">back-forward-cache-restoration<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2718<\/td>\n<td align=\"center\">\u2718<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">element<\/td>\n<td align=\"center\">\u5143\u7d20\u52a0\u8f7d\u65f6\u95f4\uff0c\u5b9e\u4f8b\u9879\u662f PerformanceElementTiming \u5bf9\u8c61\u3002<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2718<\/td>\n<td align=\"center\">\u2718<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">event<\/td>\n<td align=\"center\">\u4e8b\u4ef6\u5ef6\u8fdf\uff0c\u5b9e\u4f8b\u9879\u662f PerformanceEventTiming \u5bf9\u8c61\u3002<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2718<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">first-input<\/td>\n<td align=\"center\">\u7528\u6237\u7b2c\u4e00\u6b21\u4e0e\u7f51\u7ad9\u4ea4\u4e92\uff08\u5373\u70b9\u51fb\u94fe\u63a5\u3001\u70b9\u51fb\u6309\u94ae\u6216\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684JavaScript\u63a7\u4ef6\u65f6\uff09\u5230\u6d4f\u89c8\u5668\u5b9e\u9645\u80fd\u591f\u54cd\u5e94\u8be5\u4ea4\u4e92\u7684\u65f6\u95f4\uff0c\u79f0\u4e4b\u4e3aFirst input delay &#8211; FID\u3002<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2718<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">largest-contentful-paint<\/td>\n<td align=\"center\">\u5c4f\u5e55\u4e0a\u89e6\u53d1\u7684\u6700\u5927\u7ed8\u5236\u5143\u7d20\uff0c\u5b9e\u4f8b\u9879\u662f LargestContentfulPaint \u5bf9\u8c61\u3002<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2718<\/td>\n<td align=\"center\">\u2718<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">layout-shift<\/td>\n<td align=\"center\">\u5143\u7d20\u79fb\u52a8\u65f6\u5019\u7684\u5e03\u5c40\u7a33\u5b9a\u6027\uff0c\u5b9e\u4f8b\u9879\u662f LayoutShift\u5bf9\u8c61\u3002<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2718<\/td>\n<td align=\"center\">\u2718<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">long-animation-frame<\/td>\n<td align=\"center\">\u957f\u52a8\u753b\u5173\u952e\u5e27\u3002<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2718<\/td>\n<td align=\"center\">\u2718<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">longtask<\/td>\n<td align=\"center\">\u957f\u4efb\u52a1\u5b9e\u4f8b\uff0c\u5f52\u5c5e\u4e8e PerformanceLongTaskTiming \u5bf9\u8c61\u3002<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2718<\/td>\n<td align=\"center\">\u2718<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">mark<\/td>\n<td align=\"center\">\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u6027\u80fd\u6807\u8bb0\u3002\u5b9e\u4f8b\u9879\u662f PerformanceMark \u5bf9\u8c61\u3002<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2714<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">measure<\/td>\n<td align=\"center\">\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u6027\u80fd\u6d4b\u91cf\u3002\u5b9e\u4f8b\u9879\u662f PerformanceMeasure \u5bf9\u8c61\u3002<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2714<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">navigation<\/td>\n<td align=\"center\">\u9875\u9762\u5bfc\u822a\u51fa\u53bb\u7684\u65f6\u95f4\uff0c\u5b9e\u4f8b\u9879\u662f PerformancePaintTiming \u5bf9\u8c61\u3002<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2714<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">paint<\/td>\n<td align=\"center\">\u9875\u9762\u52a0\u8f7d\u65f6\u5185\u5bb9\u6e32\u67d3\u7684\u5173\u952e\u65f6\u523b\uff08\u7b2c\u4e00\u6b21\u7ed8\u5236\uff0c\u7b2c\u4e00\u6b21\u6709\u5185\u5bb9\u7684\u7ed8\u5236\uff0c\u5b9e\u4f8b\u9879\u662f PerformancePaintTiming \u5bf9\u8c61\u3002<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2714<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">resource<\/td>\n<td align=\"center\">\u9875\u9762\u4e2d\u8d44\u6e90\u7684\u65f6\u95f4\u4fe1\u606f\uff0c\u5b9e\u4f8b\u9879\u662f PerformanceResourceTiming \u5bf9\u8c61\u3002<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2714<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">taskattribution<\/td>\n<td align=\"center\">\u957f\u671f\u4efb\u52a1\u6709\u91cd\u5927\u8d21\u732e\u7684\u5de5\u4f5c\u7c7b\u578b\uff0c\u5b9e\u4f8b\u9879\u662f TaskAttributionTiming \u5bf9\u8c61\u3002<\/td>\n<td align=\"center\">\u2718<\/td>\n<td align=\"center\">\u2718<\/td>\n<td align=\"center\">\u2718<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">soft-navigation<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2718<\/td>\n<td align=\"center\">\u2718<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">visibility-state<\/td>\n<td align=\"center\">\u9875\u9762\u53ef\u89c1\u6027\u72b6\u6001\u66f4\u6539\u7684\u65f6\u95f4\uff0c\u5373\u9009\u9879\u5361\u4f55\u65f6\u4ece\u524d\u53f0\u66f4\u6539\u4e3a\u540e\u53f0\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u5b9e\u4f8b\u9879\u662f VisibilityStateEntry \u5bf9\u8c61\u3002<\/td>\n<td align=\"center\">\u2714<\/td>\n<td align=\"center\">\u2718<\/td>\n<td align=\"center\">\u2718<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>PS\uff1a\u6d4b\u8bd5\u6d4f\u89c8\u5668\uff0cChrome 114\uff0cFirefox 116 \u548c Safari 16.5.2.<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u57fa\u672c\u4e0a\uff0c\u6bcf\u4e00\u79cd\u6027\u80fd\u6307\u6807\u90fd\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684\u4e13\u5c5e\u5bf9\u8c61\u7c7b\u578b\u3002<\/p>\n<p>\u5176\u4e2d\uff0c\u6709\u4e9b\u6307\u6807\u5c5e\u4e8e\u6838\u5fc3\u6307\u6807\u3002<\/p>\n<h4>Web\u6027\u80fd\u6838\u5fc3\u6307\u6807<\/h4>\n<ul>\n<li>LCP\uff0cLargest Contentful Paint\uff0c\u5bf9\u5e94largest-contentful-paint\u7c7b\u578b\uff0c\u662f\u52a0\u8f7d\u6027\u80fd\u6307\u6807\u3002<\/li>\n<li>FID\uff0cFirst Input Delay\uff0c\u5bf9\u5e94first-input\u7c7b\u578b\uff0c\u662f\u4ea4\u4e92\u6027\u80fd\u6307\u6807\u3002<\/li>\n<li>CLS\uff0cCumulative Layout Shift\uff0c\u5bf9\u5e94layout-shift\u7c7b\u578b\uff0c\u662f\u89c6\u89c9\u7a33\u5b9a\u6027\u6307\u6807\u3002<\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u91cd\u70b9\u5173\u6ce8\u4e0b\uff0c\u81f3\u4e8e\u5176\u4ed6\u4e00\u4e9b\u65f6\u95f4\u7c7b\u578b\uff0c\u53ca\u5176\u5bf9\u5e94\u7684\u5bf9\u8c61\uff0c\u54b3\u54b3\u2026\u2026\u5185\u5bb9\u5b9e\u5728\u592a\u591a\u4e86\uff0c\u5b66\u4e0d\u5b8c\uff0c\u5b9e\u5728\u662f\u5b66\u4e0d\u5b8c\uff0c\u6240\u4ee5\uff0c\u7565\u8fc7\uff0c\u7b49\u54ea\u5929\u771f\u6b63\u9700\u8981\u4e86\uff0c\u53ef\u4ee5\u518d\u597d\u597d\u770b\u4e00\u770b\u3002<\/p>\n<h3>\u56db\u3001paint\u7c7b\u578b\u4e0e\u9996\u6b21\u6e32\u67d3\u65f6\u95f4<\/h3>\n<p>\u4e0b\u9762\u770b\u4e0bPerformanceObserver\u4e2d &#8216;paint&#8217; \u7684\u8fd0\u884c\u7ed3\u679c\u3002<\/p>\n<p>\u770b\u4e0b\u9762\u7684\u4ee3\u7801\u548c\u5b9e\u4f8b\uff0c\u5728\u9875\u9762\u5934\u90e8\u5199\u5165\uff1a<\/p>\n<pre>const observer = new PerformanceObserver(entryList => {\r\n    for (const entry of entryList.getEntries()) {\r\n        console.dir(entry);\r\n    }\r\n});\r\nobserver.observe({\r\n  entryTypes: ['paint']\r\n});<\/pre>\n<p>\u5728Chrome\u6d4f\u89c8\u5668\u4e0b\uff0c\u4f1a\u8f93\u51fa\u4e24\u4e2a\u6761\u76ee\u5bf9\u8c61\uff0c\u5982\u4e0b\u622a\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202308\/2023-08-24_142216.png\" width=\"354\" height=\"271\" alt=\"Chrome\u4e0b paint \u6761\u76ee\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5206\u522b\u662f\u5f00\u59cb\u6e32\u67d3\u548c\u5f00\u59cb\u6709\u5185\u5bb9\uff08\u6587\u5b57\u6216\u56fe\u7247\u6240\u5728DOM\uff09\u6e32\u67d3\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6709\u5185\u5bb9\u6e32\u67d3\u7684\u6570\u636e\u7ed3\u6784\uff1a<\/p>\n<pre>{\r\n  \"name\":\"first-contentful-paint\",\r\n  \"entryType\":\"paint\",\r\n  \"startTime\":220.80000001192093,\r\n  \"duration\":0\r\n}<\/pre>\n<p>Firefox\u4e0b\u5219\u53ea\u6709\u4e00\u4e2a\u6761\u76ee\uff0c\u56e0\u4e3a\u652f\u6301\u7684\u7c7b\u578b\u66f4\u5c11\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202308\/2023-08-24_142557.png\" width=\"573\" height=\"239\" alt=\"Firefox\u4e0b\u7684\u652f\u6301\u60c5\u51b5\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5176\u4e2d\u53ef\u53c2\u8003\u7684\u6307\u6807\u5c31\u662f startTime\uff0c\u8868\u793a\u4ec0\u4e48\u65f6\u5019\u5185\u5bb9\u5f00\u59cb\u6e32\u67d3\uff0c\u57fa\u672c\u4e0a\uff0c\u53ef\u4ee5\u8fd1\u4f3c\u8868\u793a\u9996\u5c4f\u5185\u5bb9\u5448\u73b0\u7684\u65f6\u95f4\u3002<\/p>\n<p>\u6839\u636e\u6211\u7684\u6d4b\u8bd5\uff0c\u7b2c\u4e00\u6b21\u8fdb\u5165\u7684\u65f6\u5019\uff0c\u65f6\u95f4\u90fd\u4f1a\u6bd4\u8f83\u957f\uff0c200ms+\uff0c\u65e0\u8bba\u54ea\u4e2a\u6d4f\u89c8\u5668\u90fd\u662f\uff0c\u7136\u540e\u518d\u5237\u65b0\u5c31\u5f88\u5feb\u4e86\uff0c\u90fd\u662f100ms\u4ee5\u5185\uff0c\u6211\u4f30\u8ba1\uff0c\u662f\u56e0\u4e3a\u7c7b\u4f3cCSS\u6587\u4ef6\u8fd9\u4e9b\u8d44\u6e90\u88ab\u7f13\u5b58\u4e86\uff08\u5916\u94feCSS\u53ef\u4ee5\u963b\u585e\u6e32\u67d3\uff09\uff0c\u6240\u4ee5\u66f4\u5feb\u4e86\u3002<\/p>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202308\/screen-paint-time-demo.php\" rel=\"noopener\" target=\"_blank\">PerformanceObserver\u9996\u5c4f\u6e32\u67d3\u65f6\u95f4demo<\/a><\/p>\n<p><span class=\"s\">\/\/zxx: Chrome\u6d4f\u89c8\u5668\u4e0bpaint\u6709\u591a\u4e2a\u7c7b\u578b\uff0c\u6211\u4eec\u5c06<code>entryList.getEntries()<\/code>\u66ff\u6362\u6210\uff08\u4f8b\u5982\uff09<code>entryList.getEntriesByName('first-contentful-paint')<\/code>\u3002<\/span><\/p>\n<h3>\u4e94\u3001\u770b\u770blargest-contentful-paint\u7684\u8fd0\u884c\u7ed3\u679c<\/h3>\n<p>\u867d\u7136LCP\u662f\u91cd\u8981\u7684\u6027\u80fd\u6307\u6807\uff0c\u4f46\u662f\u76ee\u524d\u4ec5Chrome\u6d4f\u89c8\u5668\u652f\u6301\u3002<\/p>\n<p>\u8fd0\u884c\u4ee3\u7801\u548c&#8217;paint&#8217;\u7c7b\u4f3c\uff0c\u8fd9\u91cc\u5c31\u4e0d\u5c55\u793a\uff0c\u611f\u5174\u8da3\u7684\u53ef\u4ee5\u731b\u51fb\u8fd9\u91cc\u8bbf\u95ee\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202308\/largest-contentful-paint-demo.php\" rel=\"noopener\" target=\"_blank\">largest-contentful-paint\u8fd0\u884c\u6d4b\u8bd5demo<\/a><\/p>\n<p>\u8f93\u51fa\u7684\u7ed3\u679c\u5f88\u5947\u602a\u3002<\/p>\n<p>\u5728\u6211\u672c\u5730\uff08\u53f3\u4e0a\u89d2\u5e7f\u544a\u56fe\u4e0d\u52a0\u8f7d\u7684\u65f6\u5019\uff09\uff0c\u6700\u5927\u5185\u5bb9\u6e32\u67d3\u5143\u7d20\u662f<code>&lt;h1&gt;<\/code>\u6807\u9898\uff0c\u7ebf\u4e0a\u5219\u662f\u53f3\u4e0a\u89d2\u7684\u5e7f\u544a\u56fe\u3002<\/p>\n<p>\u4ee5\u53ca\uff0c\u518d\u8f93\u51fa\u7684\u662fpre\u5143\u7d20\uff0c\u5e76\u4e14\u662f\u5728\u5b9a\u65f6\u5668\u89e6\u53d1\u5185\u5bb9\u66f4\u65b0\u540e\u663e\u793a\u3002<\/p>\n<p>\u6309\u7167\u6211\u7684\u7406\u89e3\uff0c\u6700\u5927\u6e32\u67d3\u5143\u7d20\u4e0d\u5e94\u8be5\u662f\u4e0b\u9762\u7684\u8272\u5f69\u6591\u6593\u7684\u5e95\u7eb9\u5927\u8272\u6846\u5417\uff1f<\/p>\n<p>\u540e\u6765\u6211\u518d\u60f3\uff0c\u5927\u5e94\u8be5\u6307\u7684\u662f\u5185\u5bb9\uff0c\u800c\u4e0d\u662f\u6e32\u67d3\u9762\u79ef\u3002<\/p>\n<p>\u4e8e\u662f\u6211\u53c8\u585e\u5165\u4e86\u5f88\u591a\u6587\u5b57\u5185\u5bb9\uff0c\u7ed3\u679c\u8fd8\u662f\u4e00\u6837\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202308\/2023-08-27_135141.png\" width=\"692\" height=\"214\" alt=\"\u6700\u5927\u5185\u5bb9\u5143\u7d20\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u8fd9\u4e2a\u5143\u7d20\u7684\u9009\u53d6\u89c4\u5219\u5b9e\u5728\u4ee4\u4eba\u8d39\u89e3\u3002<\/p>\n<p>\u4e8e\u662f\u6211\u53c8\u4ed4\u7ec6\u67e5\u770b\u4e86\u4e0bMDN\u6587\u6863\uff0c\u5927\u81f4\u77e5\u9053\u539f\u56e0\u4e86\u3002<\/p>\n<blockquote><p>The LargestContentfulPaint interface provides timing information about the largest image or text paint before user input on a web page.<\/p><\/blockquote>\n<p>\u9700\u8981\u662f\u5728\u9875\u9762\u53ef\u4ea4\u4e92\u4e4b\u524d\u6e32\u67d3\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u8981\u5728\u89c6\u533a\u8303\u56f4\u4e4b\u5185\uff08\u5143\u7d20\u6240\u6709\u533a\u57df\uff09\u3002<\/p>\n<p>\u57fa\u4e8e\u8fd9\u4e24\u70b9\uff0c\u6211\u53c8\u91cd\u65b0\u4fee\u6539\u4e86\u4e0b\u9875\u9762\u5185\u5bb9\uff0c\u5305\u62ec\u9ad8\u5ea6\u51cf\u5c0f\uff0c\u5185\u5bb9\u589e\u591a\uff0c\u7136\u540e\u4e00\u5237\u65b0\uff0c\u54e6\u543c\uff0c\u6210\u4e86\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202308\/2023-08-27_140949.png\" width=\"617\" height=\"194\" alt=\"\u6700\u5927\u6e32\u67d3\u5185\u5bb9\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5f00\u5fc3\uff0c\u53c8\u5f04\u660e\u767d\u4e00\u4e2a\u5c0f\u77e5\u8bc6\u3002<\/p>\n<h3>\u516d\u3001\u8fc7\u53bb\u4e0d\u592a\u65b9\u4fbf\u83b7\u53d6\u7684first-input\u6307\u6807<\/h3>\n<p>first-input\u6307\u7684\u662f\u7528\u6237\u9875\u9762\u9996\u6b21\u4ea4\u4e92\u65f6\u95f4\uff0c\u6211\u4e00\u5f00\u59cb\u8fd8\u89c9\u5f97\u662f\u53ef\u4ea4\u4e92\u65f6\u95f4\uff0c\u5e78\u597d\u6d4b\u8bd5\u4e86\u4e0b\u3002<\/p>\n<pre>const observer = new PerformanceObserver(entryList =&gt; {\r\n    for (const entry of entryList.getEntries()) {\r\n        console.dir(entry);\r\n    }\r\n});\r\nobserver.observe({\r\n  entryTypes: ['first-input']\r\n});<\/pre>\n<p>\u4f8b\u5982\uff0c\u4e0a\u9762\u7684\u6d4b\u8bd5\u4ee3\u7801\uff0c\u5728\u9875\u9762\u5237\u65b0\u540e\uff0c\u662f\u6ca1\u6709\u4efb\u4f55\u8f93\u51fa\u7684\u3002<\/p>\n<p>\u53ea\u6709\u5f53\u4f60\u70b9\u51fb\u9875\u9762\uff0cconsole\u8bed\u53e5\u624d\u4f1a\u6267\u884c\u3002<\/p>\n<p>\u8fd9\u4e2aAPI\u5012\u662f\u53ef\u4ee5\u7528\u6765\u7edf\u8ba1\u9875\u9762\u662f\u5426\u88ab\u7528\u6237\u4ea4\u4e92\u8fc7\uff0c\u4ee5\u53ca\u505c\u7559\u65f6\u95f4\uff0c\u4e5f\u4e0d\u77e5\u9053\u53ef\u4e0d\u53ef\u4ee5\u7528\u6765\u8bc6\u522b\u662f\u5426\u662f\u673a\u5668\u884c\u4e3a\u3002<\/p>\n<p>\u4ee5\u540e\u6709\u673a\u4f1a\u8bd5\u4e00\u4e0b\u3002<\/p>\n<p>\u5176\u8f93\u51fa\u7ed3\u679c\u793a\u610f\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202308\/2023-08-27_150230.png\" width=\"435\" height=\"265\" alt=\"\u8d77\u6b62\u65f6\u95f4\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<pre>{\r\n    \"name\": \"pointerdown\",\r\n    \"entryType\": \"first-input\",\r\n    \"startTime\": 2953.4000000953674,\r\n    \"duration\": 0,\r\n    \"navigationId\": 1,\r\n    \"processingStart\": 2953.7000002861023,\r\n    \"processingEnd\": 2953.7000002861023,\r\n    \"cancelable\": true\r\n}<\/pre>\n<p>\u5176\u4e2dstartTime\u5c31\u662f\u4ece\u9875\u9762\u5f00\u59cb\u53ef\u4ea4\u4e92\uff0c\u5230\u7b2c\u4e00\u6b21\u4ea4\u4e92\u7684\u65f6\u95f4\u95f4\u9694\uff0cname\u662f\u4e8b\u4ef6\u7c7b\u578b\uff0c\u70b9\u51fb\u662fpointerdown\uff0c\u952e\u76d8\u8bbf\u95ee\u662fkeydown\u3002<\/p>\n<p>\u6211\u6d4b\u8bd5\u4e86\u4e0b\uff0c\u5728PC\u7aef\uff0c\u4f3c\u4e4e\u53ea\u6709\u70b9\u51fb\u548c\u952e\u76d8\u65b9\u4f4d\u624d\u80fd\u89e6\u53d1\uff0c\u9f20\u6807hover\u63a7\u4ef6\u5143\u7d20\uff0c\u4ee5\u53ca\u9f20\u6807\u6eda\u8f6e\u6eda\u52a8\u90fd\u662f\u4e0d\u4f1a\u89e6\u53d1\u7684\u3002<\/p>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202308\/js-first-input-demo.php\" rel=\"noopener\" target=\"_blank\">first-input\u9996\u6b21\u4ea4\u4e92\u65f6\u95f4\u6d4b\u8bd5demo<\/a><\/p>\n<h3>\u4e03\u3001\u63a5\u4e0b\u6765\u662fmark\u548cmeasure<\/h3>\n<p>Web\u4e2d\u7684Performance\u4e2d\u6027\u80fd\u90fd\u662f\u56f4\u7ed5\u65f6\u95f4\u7ebftimeline\u5c55\u5f00\u7684\uff0c\u3002<\/p>\n<p>\u4eceChrome\uff0cFirefox\u7b49\u6d4f\u89c8\u5668\u7684\u6027\u80fd\u5206\u6790\u5de5\u5177\u53ef\u4ee5\u770b\u51fa\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202308\/2023-08-16_001342.png\" width=\"706\" height=\"172\" alt=\"\u65f6\u95f4\u7ebf\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u800c\u9700\u8981\u77e5\u9053\u67d0\u4e00\u6bb5\u65f6\u95f4\u7ebf\u7684\u6027\u80fd\uff0c\u5c31\u9700\u8981\u77e5\u9053\u8fd9\u4e2a\u65f6\u95f4\u7ebf\u7684\u8d77\u6b62\u70b9\u3002<\/p>\n<p>\u8fd9\u4e2a\u8d77\u6b62\u70b9\u7684\u6807\u8bb0\u5c31\u53eb\u505amark\uff0c\u671f\u95f4\u7684\u6027\u80fd\u6d4b\u91cf\u5c31\u662fmeasure\u3002<\/p>\n<p>\u8fd9\u4e24\u4e2a\u52a8\u4f5c\u5bf9\u5e94\u4e24\u4e2a\u4e13\u95e8\u7684\u5bf9\u8c61\u63a5\u53e3\uff0c\u524d\u8005\u662fPerformanceMark\uff0c\u53ef\u4ee5\u7ed9\u4efb\u610f\u4f4d\u7f6e\u547d\u540d\u548c\u6dfb\u52a0\u6807\u8bb0\uff0c\u540e\u8005\u662fPerformanceMeasure\uff0c\u662f\u4e24\u4e2a\u6807\u8bb0\u4e4b\u95f4\u7684\u65f6\u95f4\u5ea6\u91cf\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u8fd9\u4e24\u4e2a\u5bf9\u8c61\u7cbe\u786e\u6d4b\u91cf\u67d0\u4e9b\u6027\u80fd\u6307\u6807\u3002<\/p>\n<p>\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528Performance API\uff0c\u8fd8\u6709\u5c31\u662f\u4f7f\u7528PerformanceObserver\u3002<\/p>\n<p>\u8fd9\u91cc\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6f14\u793a\u4e0b\u5404\u81ea\u7684\u4f7f\u7528\u3002<\/p>\n<p>\u5047\u8bbe\u9875\u9762\u4e0a\u6709\u4e24\u4e2a\u5143\u7d20\uff0c\u4e00\u4e2aid\u662foutput\uff0c\u53e6\u5916\u4e00\u4e2a\u662ffooter\uff0c\u5219\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\uff08\u4f7f\u7528\u5b9a\u65f6\u5668\u6a21\u62df\u65f6\u95f4\u95f4\u9694\uff09\uff1a<\/p>\n<pre>\/\/ mark\u6807\u8bb0\r\nperformance.mark('test-start', {\r\n    startTime: 0,\r\n      detail: { htmlElement: 'output' }\r\n});\r\n\r\nsetTimeout(() =&gt; {\r\n    performance.mark('test-end', {\r\n        startTime: 360,\r\n        detail: { htmlElement: 'footer' }\r\n    });    \r\n    \r\n    const testMeasure = performance.measure(\r\n      \"test-start\",\r\n      \"test-end\"\r\n    );\r\n    \r\n    console.dir(testMeasure);\r\n}, 360);\r\n\r\nconst observer = new PerformanceObserver(entryList =&gt; {\r\n    entryList.getEntries().forEach((entry) =&gt; {\r\n        var logMark = '';\r\n        var logMeasure = '';\r\n        if (entry.entryType === 'mark') {\r\n              logMark = `${entry.name}\u7684startTime\u662f: ${entry.startTime}`;\r\n              console.log(logMark);\r\n        }\r\n        if (entry.entryType === 'measure') {\r\n              console.log(logMeasure = `${entry.name}\u7684duration\u65f6\u95f4\u662f: ${entry.duration}`);\r\n        }\r\n    });\r\n});\r\nobserver.observe({\r\n    entryTypes: ['mark', 'measure']\r\n});<\/pre>\n<p>\u6b64\u65f6\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u6267\u884c\u5f00\u59cb\u65f6\u95f4\uff0c\u548c\u65f6\u95f4\u95f4\u9694\u7b49\u4fe1\u606f\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202308\/2023-08-27_160827.png\" width=\"393\" height=\"249\" alt=\"\u6e32\u67d3\u65f6\u95f4\u67e5\u770b\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6b64\u65f6\u7684 duration \u5e94\u8be5\u5c31\u662f\u4e24\u4e2aDOM\u5143\u7d20\u4e4b\u95f4\u7684\u6e32\u67d3\u65f6\u95f4\u4e86\u5427\u3002<\/p>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202308\/js-mark-neasure-demo.php\" rel=\"noopener\" target=\"_blank\">mark\u548cmeasure\u7c7b\u578b\u6d4b\u8bd5demo<\/a><\/p>\n<p>\u76f8\u6bd4\u4e0eDate.now()\u6216\u8005\u4f7f\u7528performance.now()\u65b9\u6cd5\u8bb0\u5f55\u65f6\u95f4\uff0c\u4f7f\u7528mark\u8fdb\u884c\u6807\u8bb0\u7684\u4f4d\u7f6e\u8fd8\u53ef\u4ee5\u548c\u5f00\u53d1\u8005\u5de5\u5177\u7684\u6027\u80fd\u9762\u677f\u914d\u5408\u4f7f\u7528\uff0c\u65b9\u4fbf\u6211\u770b\u67e5\u770b\u8be6\u60c5\uff0c\u6709\u52a9\u4e8e\u6211\u4eec\u5b9a\u4f4d\u95ee\u9898\uff0c\u4ee5\u53ca\u7406\u89e3Web\u9875\u9762\u7684\u52a0\u8f7d\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4e0a\u9762\u7684demo\u9875\u9762\u4e2d\uff0c&#8217;test-start&#8217;\u8fd9\u4e2a\u6807\u8bb0\u5c31\u53ef\u4ee5\u5728\u4e8b\u4ef6\u65f6\u95f4\u7ebf\u4e2d\u770b\u5230\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202308\/2023-08-27_224711.png\" width=\"286\" height=\"447\" alt=\"\u65f6\u95f4\u7ebf\u622a\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u516b\u3001\u597d\u4e86\uff0c\u6253\u4f4f\uff0c\u8be5\u7ed3\u675f\u4e86<\/h3>\n<p>\u7ecf\u8fc7\u8fd9\u4e00\u6ce2\u9605\u8bfb\uff0c\u6d4b\u8bd5\u548c\u64b0\u5199\uff0c\u6211\u5df2\u7ecf\u5927\u81f4\u641e\u6e05\u695aPerformanceObserver\u7684\u82b1\u82b1\u8349\u8349\u4e86\u2026\u2026\u55ef\u55ef\u55ef\u2026\u2026\u662f\u8fd9\u6837\u7684\uff0c\u7b49\u54ea\u4e00\u5929\u9047\u5230\u9875\u9762\u5361\u987f\uff0c\u53ef\u4ee5\u7528\u8fd9\u4e2a\u6392\u67e5\u770b\u770b\u3002<\/p>\n<p>\u81f3\u4e8e\u6027\u80fd\u7edf\u8ba1\u4e0e\u4e0a\u62a5\u4e5f\u662f\u4e2a\u4e0d\u5e38\u89c1\u7684\u9700\u6c42\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u4e86\u89e3\u4e0b\u5c31\u597d\u4e86\u3002<\/p>\n<p>\u5e73\u5e38\u90a3\u4e9b\u9879\u76ee\uff0c\u53ea\u8981\u6b63\u5e38\u5f00\u53d1\uff0c\u4ee5\u76ee\u524d\u7684\u6d4f\u89c8\u5668\u6027\u80fd\uff0c\u90fd\u4f1a\u5f88\u6d41\u7545\u7684\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u8fd8\u662f\u8981\u4eb2\u81ea\u6d4b\u8bd5\uff0c\u53ea\u9760\u770b\u6587\u6863\uff0c\u5f88\u591a\u4e1c\u897f\u662f\u770b\u4e0d\u51fa\u6765\u7684\uff0c\u5e76\u4e14\u6587\u6863\u5f88\u591a\u5185\u5bb9\u90fd\u662f\u7f3a\u5931\u7684\u3002<\/p>\n<p>OK\uff0c\u9a6c\u4e0a9\u6708\u4e86\uff0c\u662f\u65f6\u5019\u62e7\u7d27\u53d1\u6761\u4e86\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u5185\u5bb9\u5df2\u7ecf\u591f\u591a\u7684\u4e86\uff0c\u5c31\u4e0d\u518d\u591a\u626f\u6de1\u4e86\u3002<\/p>\n<p>\u611f\u8c22\u60a8\u7684\u9605\u8bfb\uff0c\u884c\u4e3a\u4ed3\u4fc3\uff0c\u5982\u6709\u9519\u8bef\uff0c\u6b22\u8fce\u6307\u6b63\u3002<\/p>\n<p>\u6bd4\u5fc3~<\/p>\n<p>? ? ? ? ?<\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10950\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=10950<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202308\/performance-obverse-cover.png\" width=\"160\" height=\"102\" alt=\"\u4e4c\u9e26\u5357\u74dc\u5c01\u9762\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>Performance\u548cPerformanceObserver\uff0c\u7528\u6237 Timing API\uff0c\u54e6\uff0c\u5b66\u4e0d\u5b8c\uff0c\u6839\u672c\u5b66\u4e0d\u5b8c\uff0c\u8fd9\u4e24\u5468\u62bd\u65f6\u95f4\u597d\u597d\u7814\u7a76\u4e86\u4e00\u756a\uff0c\u7ec8\u4e8e\u4e86\u89e3\u4e86\u4e2a\u5927\u6982\u4e86?\uff0c\u7279\u6b64\u8bb0\u5f55\u4e0b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[1691,1478,1370,1941,1940,1572,443],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/10950"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=10950"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/10950\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=10950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=10950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=10950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}