-
Notifications
You must be signed in to change notification settings - Fork 3
/
usertiming-compression.min.js
1 lines (1 loc) · 3.97 KB
/
usertiming-compression.min.js
1
!function(r){"use strict";var e,n;void 0!==r?(e=r,n=e.UserTimingCompression):e={};var t,i=t={};i.noConflict=function(){return e.UserTimingCompression=n,i},i.trimTiming=function(r,e){"number"!=typeof r&&(r=0),"number"!=typeof e&&(e=0);var n=Math.round(r),t=Math.round(e);return 0===n?0:n-t},i.toBase36=function(r){return"number"==typeof r?r.toString(36):""},i.findUserTimingForFrame=function(r){var e;if(!r)return[];try{r.location&&r.location.href;if(!("performance"in r&&r.performance&&r.performance.getEntriesByType))return e;e=r.performance.getEntriesByType("mark"),e=e.concat(r.performance.getEntriesByType("measure"))}catch(r){return e}return e},i.compressUserTiming=function(r,e){var n,i,o,a=0,u={};if(e=e||{},!r||!r.length)return[];for(n=0;n<r.length;n++)i=r[n],void 0===u[i.name]&&(u[i.name]=[]),"mark"===i.entryType?u[i.name].push({startTime:i.startTime}):"measure"===i.entryType&&u[i.name].push({startTime:i.startTime,duration:i.duration});for(var f in u)if(u.hasOwnProperty(f)){if(e.map&&void 0===e.map[f])continue;var s=u[f];for(e.map&&void 0!==e.map[f]&&(delete u[f],f=e.map[f]),a=0,n=0;n<s.length;n++){var m=s[n];o=t.toBase36(t.trimTiming(m.startTime,a)),"0"===o&&(o="");var c=o;if("number"==typeof m.duration){var p=t.toBase36(Math.round(m.duration));c+="_","0"!==p&&(c+=p)}a=m.startTime,s[n]=c}u[f]=t.compressArray(s)}return u},i.convertToTrie=function(r){var e,n,t,i,o,a,u,f={};if(!r)return{};for(e in r)if(r.hasOwnProperty(e))for(t=r[e],i=e.split(""),a=f,n=0;n<i.length;n++)o=i[n],u=a[o],void 0===u?a=a[o]=n===i.length-1?t:{}:"string"==typeof u||"number"==typeof u?a=a[o]={"!":u}:n===i.length-1?a[o]["!"]=t:a=a[o];return f},i.optimizeTrie=function(r,e){var n,t,i,o=0;if(!r)return{};for(n in r)"object"==typeof r[n]&&(t=this.optimizeTrie(r[n],!1))&&(delete r[n],n+=t.name,r[n]=t.value),o++;return 1===o?e?(i={},i[n]=r[n],i):{name:n,value:r[n]}:!!e&&r},i.compressArray=function(r){var e=0,n="";if(!r||0===r.length||r.constructor!==Array)return"";for(var t=0;t<r.length;t++){var i=r[t];t<r.length-1&&i===r[t+1]?e++:e>0?(n+=(""!==n?".":"")+i+"*",e>=2&&(n+=e+1),e=0):n+=(""!==n?".":"")+i}return/^\d+$/.test(n)?parseInt(n,10):n},i.getCompressedUserTiming=function(e){var n,i;return e=e||{},n=e.window||r,i=this.findUserTimingForFrame(n),e.from&&(i=i.filter(function(r){return r.startTime+r.duration>=e.from})),e.to&&(i=i.filter(function(r){return r.startTime<=e.to})),t.compressUserTiming(i,e)},i.compressForUri=function(r){if("object"!=typeof r)return"";var e=!1;for(var n in r)if(r.hasOwnProperty(n)){if(isNaN(n)){e=!1;break}e=!0}if(e)return"1"+t.flattenMap(r);var i=t.convertToTrie(r),o=t.optimizeTrie(i,!0),a=t.jsUrl(o),u=t.flattenArray(r);if("string"!=typeof u||0===u.length)return"";var f=encodeURIComponent(a),s=encodeURIComponent(u);return f.length<s.length?a:"0"+u},i.flattenArray=function(r){var e=[];if("object"!=typeof r)return"";for(var n in r)if(r.hasOwnProperty(n)){var t=(r[n]+"").replace("~","%7E");n=n.replace("~","%7E"),e.push(n+"~"+t)}return e=e.join("~")},i.flattenMap=function(r){var e=[];if("object"!=typeof r)return"";for(var n in r)if(r.hasOwnProperty(n)){var i=parseInt(n,10),o=t.toBase36(i);if(i>1331)continue;o.length>1&&(o=t.toBase36(i-36),o="-"+(1===o.length?"0":"")+o),e.push(o+r[n])}return e.sort(),e=e.join("~")},i.jsUrl=function(r){function e(r){return/[^\w-.]/.test(r)?r=r.replace(/[^\w-.]/g,function(r){return"$"===r?"!":(r=r.charCodeAt(0),r<256?"*"+("00"+r.toString(16)).slice(-2):"**"+("0000"+r.toString(16)).slice(-4))}):r}switch(typeof r){case"number":return isFinite(r)?"~"+r:"~null";case"string":return"~'"+e(r);case"boolean":return"~"+r;case"object":return r?Array.isArray(r)?"~("+(r.map(function(r){return t.jsUrl(r)||"~null"}).join("")||"~")+")":"~("+Object.keys(r).map(function(n){var i=t.jsUrl(r[n]);return i&&e(n)+i}).filter(function(r){return r}).sort().join("~")+")":"~null";default:return}},"function"==typeof define&&define.amd?define([],function(){return i}):"undefined"!=typeof module&&module.exports?module.exports=i:void 0!==e&&(e.UserTimingCompression=i)}("undefined"!=typeof window?window:void 0);