I have an idea: why not speed up your site by NOT running js at all?
Show HN: Speed up your site by running JavaScript when the browser is idle
21–30 of 95 posts
Re: Show HN: Speed up your site by running JavaScript when the browser is idle
#22Some 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
#23I have an idea: why not speed up your site by NOT running js at all?
Re: Show HN: Speed up your site by running JavaScript when the browser is idle
#24Earlier 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.
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
#25MDN has a good example and looks like this is not supported by Safari -
https://developer.mozilla.org/en-US/docs/Web/API/Background_...
Re: Show HN: Speed up your site by running JavaScript when the browser is idle
#26Couldn’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.
Re: Show HN: Speed up your site by running JavaScript when the browser is idle
#27Re: Show HN: Speed up your site by running JavaScript when the browser is idle
#28Of 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
#29While 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…