Search code examples
javascriptphantomjswebpage-screenshot

Phantomjs works but is very slow


I am trying to take a screenshot of a webpage with PhantomJS. Specifically, I am using the example of capturing espn.com from this example. My code looks like this:

var page = new WebPage(); 
    page.open('http://www.espn.com', function (status) {
    page.render('fb.png');
    phantom.exit();
});

I then go to my PhantomJS directory with either my terminal or command prompt and run:

phantomjs shotty.js

Everything runs great, however it takes 6-8 seconds to complete the output image. Is that normal? Is there a faster way to accomplish this so that it completes in a second or less?

I am using CentOS and Windows 7. Both boxes have 8GB of RAM, 3.2 GHz CPU, and I'm getting 22Mbp/s down and 1Mbp/s up on speedtest.net


Solution

  • Yes this is normal. When you attempt to render, PhantonJS will still wait for the page.open event to fire the load event to signify that the entire DOM has been loaded.

    Take a look at what happens when I load espn.com locally on my system. It takes ~2 seconds for DOMContentLoaded to finish, and then ~7 seconds for the ready event to fire.

    enter image description here