Live data from Hacker News

Show HN: Speed up your site by running JavaScript when the browser is idle

npmjs.com

21–30 of 95 posts

Re: Show HN: Speed up your site by running JavaScript when the browser is idle

#22
The code: https://github.com/hiroki0525/idle-task/blob/main/src/index....

Some initial impressions (based on https://github.com/hiroki0525/idle-task/blob/7e88c8b97c926cf...):

• The cancelIdleCallback fallback implementation will never be defined, so cancelAllIdleTasks will unexpectedly throw an exception in environments like Safari that depend on the polyfill. (Solution: delete lines 14–15.)

• That the simple fallback implementation is a modifying polyfill is not nice, if not clearly stated. Typically better to define it as a local, e.g. `const rIC = typeof requestIdleCallback !== "undefined" ? requestIdleCallback : function …` and subsequently use rIC instead of requestIdleCallback. (Related, I’m not sure quite why you only install the polyfill if self is a thing, are you deliberately ensuring you can’t use this in Node?)

• The default code path is broken, process.env.NODE_ENV will fail in normal browser environments since process is undefined. (And no, no build process gets rid of this, fetch the distributed package and see that its index.js still contains it.)

• Line 74, don’t return a value from a private function if none of the uses use that value; just drop the return keyword.

• Lines 86–93, splitting logArgs out doesn’t make sense to me, it’s functionally harmless, but needless bloat. (I am perennially disappointed at the utterly primitive state of JavaScript bundlers/optimisers/minifiers: especially with TypeScript knowledge of the interfaces being touched, reordering and inlining is a trivial and obvious optimisation that only changes behaviour on bonkers code that deserves to get broken, but no tool will do it, though Closure Compiler’s advanced optimisations mode might be able to be coaxed into doing it—if it even supports spread syntax.)

• The vibe I’m getting is that this works with a mixture of callbacks and promises, and ends up complicated because of it, parts of it feeling like the ’90s and parts like the mid-’10s. The modern approach would be to use abort signals, which… eh, they’re simpler in some cases, more complicated in others. But from this library’s perspective, you could replace the entire interface with just one promise-returning function, which I’d name schedule(task, options), with options including {signal: AbortSignal}. Cancellation would be handled a bit differently, but it’d be more flexible, because it’s left up to the caller how they hook cancellation up (want to be able to cancel a subset of all the tasks together? Easy, give them the same signal). And I think the code of this library would be simplified by going all in on such a pattern.

Re: Show HN: Speed up your site by running JavaScript when the browser is idle

#24
post #9

Earlier quoted context omitted.

You mean my browser still doesn't use enough CPU? I don't want sites that aren't in focus to do anything.

The point is to play nice. "I have this task the page needs to do, but I can wait a bit until more convenient". Not "browser is idle, let's mine some bitcoin" as you suggest.

Actually I wasn't thinking of bitcoin, but for example of needless auto refreshes and animations.

Seriously, web devs need to learn to respect my battery and electricity bill more.

Re: Show HN: Speed up your site by running JavaScript when the browser is idle

#26

Couldn’t this have negative impact on user battery life depending on the weight of the JS or number of open tabs with sites using it? Seems like it might be antagonistic to host OS efforts to to coalesce work and perform it while the CPU is already awake to reduce the number of wakeups and save power, since it specifically waits for browser idle.

This is for site performance and not for general OS performance.

"Does your computer run out of battery 10x faster while on the site" should be a site performance issue if it's something in the sites js thats eating the battery.

Re: Show HN: Speed up your site by running JavaScript when the browser is idle

#27
post #6

I have an idea: why not speed up your site by NOT running js at all?

you are absolutely right. we should completely throw away the development of browser APIs over the years and develop sites with html 4.0

This but unironically.

Re: Show HN: Speed up your site by running JavaScript when the browser is idle

#28
While unrelated to this, I would like browsers to suspend background tabs and maybe even compress their data and unload information that can be restored later (for example, unload images from memory, drop JS bytecode and JIT caches). Majority of websites have no need to do anything when I am not watching them.

Of course there are cases when you need some background activity, for example if you are listening to music or running a program. As I understand, it is extremely difficult do distuinguish between tabs doing useful work and not doing. I would be fine with a manual switch to allow site run in background.

Re: Show HN: Speed up your site by running JavaScript when the browser is idle

#29

While unrelated to this, I would like browsers to suspend background tabs and maybe even compress their data and unload information that can be restored later (for example, unload images from memory, drop JS bytecode and JIT caches). Majority of websites have no need to do anything when I am not watching them. Of course there are cases when you need some background activity, for example if you are listening to music…

I'm pretty sure Safari does this, as returning to a heavy tab (say, google sheets) after a long period of it having been in the background takes a few seconds on my 2015 MacBook Pro. I assume that time is spent deserializing the tab’s state.

Re: Show HN: Speed up your site by running JavaScript when the browser is idle

#30
post #6

I have an idea: why not speed up your site by NOT running js at all?

you are absolutely right. we should completely throw away the development of browser APIs over the years and develop sites with html 4.0

Totally agree with this, actually.
Post reply on HN