This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-03-23
Channels
- # babashka (15)
- # beginners (102)
- # biff (4)
- # calva (15)
- # cider (51)
- # clojure (5)
- # clojure-dev (3)
- # clojure-europe (3)
- # clojure-france (1)
- # clojure-india (1)
- # clojure-korea (2)
- # clojure-norway (13)
- # clojurescript (20)
- # data-science (1)
- # datalevin (6)
- # datascript (2)
- # emacs (3)
- # events (2)
- # fulcro (4)
- # gratitude (2)
- # introduce-yourself (8)
- # lsp (3)
- # malli (1)
- # meander (1)
- # nbb (9)
- # off-topic (11)
- # releases (1)
- # ring (1)
- # yamlscript (5)
Is it possible to limit or capture (in a variable) the execution time of an expression in clojurescript? I've got an expression that determines the resolution of some graphics, which is essentially (iterate f x), that could in theory go on indefinitely, but in practice the time taken by each iteration grows exponentially. When the page loads, i'd like to iterate as many times as possible, but stop at the first step taking more than 1-2 seconds. My shitty phone can only handle 6 steps for instance, while my computer can easily do 9, which is why i'd like it to load at a better resolution on more capable devices. Any ideas how i should approach this?
Not sure this is the right approach, but i guess i could just get the current time before and after the call? :man-facepalming:
You sure can. That approach has one problem - the whole UI will be completely unresponsive (apart maybe from some scrolling - depends on the browser and its settings) until you're done.
With js/requestAnimationFrame
, you will at least give your user a chance to do something in the meantime.
I can live with an unresponsive UI (for now), there isn't that much to do while waiting either way. But i'll bookmark js/requestAnimationFrame
for when it does become an issue!
Now i have the issue that in
(let
[a (.now js/Date)
x (expensive-call)
b (.now js/Date)])
a and b are identical. What am i missing, something about laziness?@U0552GV2X32 may be worth checking out the performance.now()
https://developer.mozilla.org/en-US/docs/Web/API/Performance/now.
That definitely seems more proper than Date.now. Searching a bit brought me this, which seems like a wrapper over both in case performance.now is unavailable https://cljs.github.io/api/cljs.core/system-time
It could lead to unexpected behaviour if you run it just as the clock switches to or from daylight saving's time
"Also, Date.now()
may have been impacted by system and user clock adjustments, clock skew, etc. as it is relative to the Unix epoch (1970-01-01T00:00:00Z) and dependent on the system clock. The performance.now()
method on the other hand is relative to the timeOrigin
property which is a https://w3c.github.io/hr-time/#dfn-monotonic-clock: its current time never decreases and isn't subject to adjustments."
Your question is about rendering, the order of magnitude is seconds. Tens of milliseconds at best when you're working with individual frames.
> It could lead to unexpected behaviour if you run it just as the clock switches to or from daylight saving's time
No. Date.now
returns the epoch time, it's not affected by your tz or any other such thing.
And i'm well aware that there's a miniscule risk of unexpected behaviour here, but there's also no cost in switching function