puppeteer: Puppeteer is working slower than PhantomJS

I have tested puppeteer vs phantomjs using http://google.com as url . Puppeteer is too slow compared to phantomjs. puppeteer = 8004 ms phantomjs = 7 ms

Code for phantomjs: phantom.js

var path = require('path')
var childProcess = require('child_process')
var phantomjs = require('phantomjs')
var binPath = phantomjs.path;
var address = process.argv[2];
var childArgs = [
	path.join(__dirname, 'phantomjs-script.js'),
  	address
];
var child = childProcess.execFile(binPath, childArgs, {timeout:60000}, function(err, stdout, stderr) {
  console.log(stdout);
})

phantomjs-script.js

var start = new Date().getTime(), end;
console.log('start: ',start);

var page = require('webpage').create(),
    system = require('system'),
    address, screenWidth, screenHeight;
try {
    address = system.args[1];
    screenWidth = 1000;
    screenHeight = 700;

    page.viewportSize = {width: screenWidth, height: screenHeight};
    page.settings.userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/52.0.2743.116 Chrome/52.0.2743.116 Safari/537.36";
    page.open(address, function (status) {
        console.log('')
        if (status !== 'success') {
            console.log('Unable to load the address!');
            phantom.exit();
        } else {
            page.render("phantom.png");
            phantom.exit();
        }
    });
} catch(e){
    phantom.exit()
}

end = new Date().getTime();
console.log('end: ', end);
console.log((end-start)/1000);

Code for puppeteer puppeteer.js

var path = require('path');
var childProcess = require('child_process');
var address = process.argv[2];
var childArgs = [
	path.join(__dirname, 'puppeteer-script.js'),
  	address,
  	foldername
];
var child = childProcess.execFile('node', childArgs, {} , function(err, stdout, stderr) {
	console.log(stdout);
});

puppeteer-script.js

var start = new Date().getTime(), end;
console.log('start: ',start);
const puppeteer = require('puppeteer');
var address, foldername, screenWidth, screenHeight;
(async() => {

	const browser = await puppeteer.launch();
	const page = await browser.newPage();	

	try {
		address = process.argv[2];
        screenWidth = 1000;
        screenHeight = 700;

		await page.setViewport({width:screenWidth, height: screenHeight});
		await page.goto(address).then(async (msg) => {
            console.log('--------------------------------------------');
            if (msg.ok !== true) {
                console.log('Unable to load the address!');
                await browser.close();
            } else {
                await page.screenshot({path: 'puppeteer.png'});
                await browser.close();                
            }
        });
    } catch(e){
        console.log('INSIDE CATCH EXCEPTION!');
        await browser.close();
	}
    end = new Date().getTime();
    console.log('end: ', end);
    console.log((end-start)/1000);

})();

I need to speed up puppeteer.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 20 (8 by maintainers)

Most upvoted comments

Guys, @abdurashidv , there is something huge you seem to have missed here :

The asynchronous nature of Javascript, and the sames applies for PhantomJS.

Exactly as what @Everettss said, the end time measure should be done within the page.open callback. Let’s remember PhantomJS main issue: the JS callback hell, and that’s why CasperJS came to the rescue…

Even if the “end = new Date().getTime();” line is written at the end of the file, it will be executed right after the “console.log('start: ',start);” line, because of the way the Javascript’s Call Stack + Event loop works. Thereby resulting in only 7ms.

A nice explained article:

@Everettss I have implemented phantom timing page.open( … ) and it is showing exact time. Phantom and puppeteer are same but in some occasions puppeteer is better than phantomjs.

@he11b0rn If you want to benchmark Puppeteer you should get rid off network layer and test pages on localhost.

For me 7ms seems a little bit too unrealistic. I don’t know how phantomjs works but check what’s the execution time if you @abdurashidv move console.log((end-start)/1000); inside page.open callback like this:

phantomjs-script.js

var start = new Date().getTime(), end;
console.log('start: ',start);

var page = require('webpage').create(),
    system = require('system'),
    address, screenWidth, screenHeight;
try {
    address = system.args[1];
    screenWidth = 1000;
    screenHeight = 700;

    page.viewportSize = {width: screenWidth, height: screenHeight};
    page.settings.userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/52.0.2743.116 Chrome/52.0.2743.116 Safari/537.36";
    page.open(address, function (status) {
        console.log('')
        if (status !== 'success') {
            console.log('Unable to load the address!');
            phantom.exit();
        } else {
            page.render("phantom.png");
            phantom.exit();
        }
        end = new Date().getTime();  //  <- move here
        console.log('end: ', end);
        console.log((end-start)/1000);
    });
} catch(e){
    phantom.exit()
}

@abdurashidv Instead of spawning a new chrome instance maybe connect to an existing already launched instance. It’s helped mine.