This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const applyBind = Function.prototype.bind.bind(Function.prototype.apply); | |
const mapSetApply = applyBind(Map.prototype.set); | |
const hijackedFns = new Map(); | |
function hijackFunction(context, propName, replacer) { | |
mapSetApply(hijackedFns, [replacer, context[propName]]); | |
context[propName] = replacer; | |
} | |
const fnToStringApply = applyBind(Function.prototype.toString); | |
const mapGetApply = applyBind(Map.prototype.get); | |
hijackFunction(Function.prototype, 'toString', function toString() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wasServedFromBrowserCache(url) { | |
const {transferSize, decodedBodySize, duration} = performance.getEntriesByName(url)[0] | |
// if we transferred bytes, it must not be a cache hit | |
// (will return false for 304 Not Modified) | |
if (transferSize > 0) return false; | |
// if the body size is non-zero, it must mean this is a | |
// ResourceTiming2 browser, this was same-origin or TAO, | |
// and transferSize was 0, so it was in the cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.addEventListener('load', function() { | |
let totalBytesSaved = 0 | |
for (const {encodedBodySize, serverTiming} of performance.getEntriesByType('resource')) { | |
for (const {name, description} of serverTiming || []) { | |
if (name === 'disk') { | |
totalBytesSaved += parseInt(description) - encodedBodySize | |
} | |
} | |
} | |
console.info('Total Bytes Saved:', totalBytesSaved) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
let userThinkTime = 0 | |
;['alert', 'confirm', 'prompt'].forEach(function(methodName) { | |
window[methodName] = (function(method) { | |
return function () { | |
userThinkTime -= performance.now() | |
var returnValue = method.apply(window, arguments) | |
userThinkTime += performance.now() | |
return returnValue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cachedInBrowser(resourceTimingEntry) { | |
if (resourceTimingEntry.requestStart) { | |
// has permissive Timing-Allow-Origin response header | |
return resourceTimingEntry.transferSize === 0 | |
} | |
// because.... physics? | |
return resourceTimingEntry.duration < 30 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!(function() { | |
const nativePO = window.PerformanceObserver | |
const polyfills = { | |
longtask: LongTaskPolyFill | |
// etc. | |
} | |
window.PerformanceObserver = (callback) => { | |
let po, registeredPolyFills = [] | |
const observe = function(options) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!(function() { | |
const nativePO = window.PerformanceObserver | |
const interfaces = { | |
resource: 'PerformanceResourceTiming', | |
longtask: 'PerformanceLongTaskTiming', | |
// etc. | |
} | |
const polyfills = { | |
longtask: LongTaskPolyFill | |
// etc. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const query = ` | |
CREATE TEMPORARY FUNCTION getHeaders(payload STRING) | |
RETURNS STRING | |
LANGUAGE js AS """ | |
try { | |
var $ = JSON.parse(payload); | |
var headers = $.response.headers; | |
var st = headers.find(function(e) { | |
return e['name'].toLowerCase() === 'server-timing' | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
if (window !== top) return; | |
const bucketSize = 500, threshold = 30, stamps = [] | |
function measureFps() { | |
const now = performance.now() | |
if (stamps.length) { | |
while (now - stamps[0] > bucketSize) { | |
stamps.shift() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isNativeFunction(fn) { | |
return typeof fn === 'function' && fn.toString && !fn.hasOwnProperty('toString') && | |
/\[native code\]/.test(String(fn)) | |
} | |
function checkNatives(fns) { | |
var results = [] | |
for (var i = 0; i < fns.length; i++) { | |
results[i] = { | |
fn: fns[i], | |
native: isNativeFunction(fns[i]) |
NewerOlder