Skip to content

Instantly share code, notes, and snippets.

@p01 p01/viewer_snippet.js
Last active Aug 29, 2015

Embed
What would you like to do?
PDF.js profile slow pages
/*
* Copy this gist into a snippet of the Sources panel of the DevTool and run it to profile slow pages
* Alternatively the PDFView.PROFILE_THRESHOLD can be set in `ms`
*/
// threshold in ms to keep/discard CPU profile
PDFView.PROFILE_THRESHOLD = 500;
PDFView.renderHighestPriority = function pdfViewRenderHighestPriority_patched(currentlyVisiblePages) {
if (PDFView.idleTimeout) {
clearTimeout(PDFView.idleTimeout);
PDFView.idleTimeout = null;
}
// Pages have a higher priority than thumbnails, so check them first.
var visiblePages = currentlyVisiblePages || this.getVisiblePages();
var pageView = this.getHighestPriority(visiblePages, this.pages,
this.pageViewScroll.down);
// start of page rendering log
var now = Date.now();
if (this.wasPageView) {
var time = now - this._when;
if (time < PDFView.PROFILE_THRESHOLD) {
// discard the previous profile and start a short lived one called 'v_v'
console.profile('v_v');
}
console.profileEnd();
console.info(time + 'ms for the page #' + this.wasPageView.id +
' ' + this.url);
this.wasPageView = null;
}
if (pageView) {
this._when = now;
this.wasPageView = pageView;
now = new Date();
console.profile(this.url +
' #' + pageView.id +
' ' + "SunMonTueWedThuFriSat".substr(now.getDay()*3, 3) +
' ' + now.toLocaleTimeString());
}
// end of page rendering log
if (pageView) {
this.renderView(pageView, 'page');
return;
}
// No pages needed rendering so check thumbnails.
if (this.sidebarOpen) {
var visibleThumbs = this.getVisibleThumbs();
var thumbView = this.getHighestPriority(visibleThumbs,
this.thumbnails,
this.thumbnailViewScroll.down);
if (thumbView) {
this.renderView(thumbView, 'thumbnail');
return;
}
}
PDFView.idleTimeout = setTimeout(function () {
PDFView.cleanup();
}, CLEANUP_TIMEOUT);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.