Skip to content

Commit

Permalink
golf: save navigator.connection to var;
Browse files Browse the repository at this point in the history
- reuses unused param name for addl 3 bytes 😇
- 710 gz / 579 br
  • Loading branch information
lukeed committed Dec 14, 2018
1 parent 1c94262 commit 5f95309
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/prefetch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,14 @@ const supportedPrefetchStrategy = typeof document !== 'undefined' && support('pr
* @param {Boolean} isPriority - if is "high" priority
* @return {Object} a Promise
*/
function prefetcher(url, isPriority) {
function prefetcher(url, isPriority, conn) {
if (preFetched[url]) {
return;
}

if ('connection' in navigator) {
// Don't prefetch if the user is on 2G...
if ((navigator.connection.effectiveType || '').includes('2g')) {
return;
}
// Don't prefetch if Save-Data is enabled...
if (navigator.connection.saveData) {
return;
}
if (conn = navigator.connection) {
// Don't prefetch if the user is on 2G. or if Save-Data is enabled..
if ((conn.effectiveType || '').includes('2g') || conn.saveData) return;
}

// Wanna do something on catch()?
Expand Down

2 comments on commit 5f95309

@kurtextrem
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this code golfing is worth it: Not 100% confident, but I think re-using an argument makes V8 work slower. Also, if not every prefetcher call includes the third argument, V8 needs to add a ArgumentsAdaptorTrampoline - see here: https://mrale.ph/blog/2018/02/03/maybe-you-dont-need-rust-to-speed-up-your-js.html#optimizing-sorting---argument-adaptation

It is out of my scope if the less bytes are worth less performance or not.

@lukeed
Copy link
Contributor Author

@lukeed lukeed commented on 5f95309 Dec 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true, but yes – I chose bytes here since the performance deopt here isn't really of concern. These calls to prefetcher happen once or twice on a page's lifecycle. It's also a background process, effectively. Squeezing every bit of req/s doesn't really matter here.

Plus the shear number of requests would be prove the network to be the bottleneck far before the deopt from the golf hack could be reached.

Please sign in to comment.