Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitgrimbo/ff1e0a01b3b55c36ad5b to your computer and use it in GitHub Desktop.
Save gitgrimbo/ff1e0a01b3b55c36ad5b to your computer and use it in GitHub Desktop.
Overcome performance.getEntries() limit of 150 resources
/**
* Simple pad string.
*/
function pad(s, len, c, left) {
while (s.length < len) s = (left ? c + s : s + c); return s;
}
/**
* Return columnar data as an array of strings.
* @param {string[][]} rows The rows/cols to format.
* @param {object} opts
* @param {number} opts.maxColWidth
* Crop columns to this width.
* @param {boolean|boolean[]} opts.padLeft
* Pad columns with spaces to left or right.
* @return {string[]} rows formatted as fixed-width columns
*/
function fixedWidthCols(rows, opts) {
function mapStringToLength(s) {
return s ? s.length : 0;
}
function getMaxColLengths(rows) {
return rows.reduce(function(cur, cols) {
var lengths = cols.map(mapStringToLength);
if (!cur) return lengths;
return cur.map(function(len, i) { return Math.max(len || 0, lengths[i] || 0)});
}, null);
}
opts = opts || {};
opts.maxColWidth = opts.maxColWidth || -1;
if ("undefined" === typeof opts.padLeft) {
opts.padLeft = true;
}
var maxColWidths = getMaxColLengths(rows);
if ("boolean" === typeof opts.padLeft) {
opts.padLeft = maxColWidths.map(function() { return opts.padLeft; });
}
return rows.map(function(row) {
return row.reduce(function(cur, col, colIdx){
var outputWidth = maxColWidths[colIdx];
if (opts.maxColWidth >= 0) {
outputWidth = Math.min(maxColWidths[colIdx], opts.maxColWidth);
}
if (col.length > outputWidth) {
col = col.substring(0, outputWidth);
}
return (cur ? cur + " " : "") + pad(col, outputWidth, " ", opts.padLeft[colIdx]);
}, null);
});
}
/**
* @param {string} b64Str
* A base 64'd cookie string (copies from Chrome dev tools and base-64'd to make it easier to
* paste back into Chrome console.)
* @param {object} opts
* Options to pass to fixedWidthCols().
*/
function cookiesAsB64StringToFixedWidthCols(b64Str, opts) {
var s = atob(b64Str);
var rows = s.split("\n").map(function(it) {
return it.split("\t");
});
return fixedWidthCols(rows, opts);
}
/*
Some code to pull more than 150 performance entries when "onresourcetimingbufferfull" or
"onwebkitresourcetimingbufferfull" events are available.
An internal entries array is maintained and added to when the either the buffer is full,
or the allEntries() function is called.
*/
allEntries = (function() {
var entries = [];
function pullEntries() {
var latestEntries = performance.getEntriesByType("resource");
entries = entries.concat(latestEntries);
var meth = "clearResourceTimings";
if (!performance[meth]) {
meth = "webkitClearResourceTimings";
}
performance[meth]();
return entries;
}
if ("performance" in window) {
performance.onresourcetimingbufferfull = performance.onwebkitresourcetimingbufferfull = pullEntries;
return pullEntries;
}
return function() { console.log("NOT SUPPORTED"); };
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment