UNPKG

@inchill/time-to-interactive

Version:

Easily measure performance metrics of time to interactive in JavaScript.

2 lines (1 loc) 3.29 kB
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).tti={})}(this,(function(n){"use strict";function t(n){return function(n){if(Array.isArray(n))return r(n)}(n)||function(n){if("undefined"!=typeof Symbol&&null!=n[Symbol.iterator]||null!=n["@@iterator"])return Array.from(n)}(n)||e(n)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function e(n,t){if(n){if("string"==typeof n)return r(n,t);var e=Object.prototype.toString.call(n).slice(8,-1);return"Object"===e&&n.constructor&&(e=n.constructor.name),"Map"===e||"Set"===e?Array.from(n):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?r(n,t):void 0}}function r(n,t){(null==t||t>n.length)&&(t=n.length);for(var e=0,r=new Array(t);e<t;e++)r[e]=n[e];return r}var o={scoring:{p10:3785,median:7300}},i=function(n){return n>o.scoring.median?"poor":n>o.scoring.p10?"needs-improvement":"good"};var a=0;n.onTTI=function(n){if(!/Safari/.test(navigator.userAgent)||/Chrome/.test(navigator.userAgent)){var r,o,u,f,c,l=0,s=[],p=[],d={name:"TTI",value:void 0===o?-1:o,rating:i(0),id:"v3-".concat(Date.now(),"-").concat(Math.floor(8999999999999*Math.random())+1e12)},y=new Map,v=function(n){y.set(n,performance.now())},m=function(n){y.delete(n)};u=v,f=m,c=window.XMLHttpRequest.prototype.open,window.XMLHttpRequest.prototype.open=function(){var n=a++;u(n),this.addEventListener("loadend",(function(){f(n)}));for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];c.apply(this,[].concat(e))},function(n,t){var e=window.fetch;window.fetch=function(){var r=a++;n(r);for(var o=arguments.length,i=new Array(o),u=0;u<o;u++)i[u]=arguments[u];return e.apply(this,[].concat(i)).then((function(n){return t(r),n}))}}(v,m);var h=new PerformanceObserver((function(n){var t,r=function(n,t){var r="undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(!r){if(Array.isArray(n)||(r=e(n))||t&&n&&"number"==typeof n.length){r&&(n=r);var o=0,i=function(){};return{s:i,n:function(){return o>=n.length?{done:!0}:{done:!1,value:n[o++]}},e:function(n){throw n},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,u=!0,f=!1;return{s:function(){r=r.call(n)},n:function(){var n=r.next();return u=n.done,n},e:function(n){f=!0,a=n},f:function(){try{u||null==r.return||r.return()}finally{if(f)throw a}}}}(n.getEntries());try{for(r.s();!(t=r.n()).done;){var o=t.value;"paint"===o.entryType&&"first-contentful-paint"===o.name&&(l=o.startTime),"longtask"===o.entryType&&(s.push(o),g()),"resource"===o.entryType&&(p.push(o),g())}}catch(n){r.e(n)}finally{r.f()}}));h.observe({entryTypes:["paint","longtask","resource"]});var g=function(){var n=s.filter((function(n){return n.startTime>=l}));if(0===n.length)b(l);else{var t=n[n.length-1];b(t.startTime+t.duration)}},w=function(e){t(y.values()).length<=2&&(d.value=e,d.rating=i(d.value),n(d),h.disconnect(),T())}}function b(n){T(),r=window.setTimeout((function(){w(n)}),5e3)}function T(){clearTimeout(r)}}}));