Skip to content

Instantly share code, notes, and snippets.

@cybermaxs
Created September 8, 2014 11:29
Show Gist options
  • Save cybermaxs/cba5b498bb50cda020cb to your computer and use it in GitHub Desktop.
Save cybermaxs/cba5b498bb50cda020cb to your computer and use it in GitHub Desktop.
Compares page load time between HTML5 Navigation timings and basic computation using Date.now() (examples/loadspeed.js). Requires PhantomJS 2.0
var page = require('webpage').create(),
system = require('system'),
t, address;
if (system.args.length === 1) {
console.log('Usage: loadspeedv2.js <some URL>');
phantom.exit(1);
} else {
t = Date.now();
address = system.args[1];
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
t = Date.now() - t;
console.log('Loading time (PhantomJS) ' + t + ' msec');
setTimeout(function() {
//wait a few ms ...
var timings = page.evaluate(function () {
return window.performance.timing;
});
console.log('Loading time (Navigation Timing) ' + (timings.loadEventEnd-timings.navigationStart) + ' msec');
phantom.exit();
}, 500);
}
});
}
//sample output
C:\phantomjs>phantomjs.exe loadspeedv2.js http://www.amazon.com/
Loading time (PhantomJS) 1862 msec
Loading time (Navigation Timing) 1861 msec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment