-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
size-suggestion: Consider removing some epoch*
getters and fromEpoch*
methods
#2849
Comments
The proposal mentions removing the IMO |
I'm neutral on this one. Probably microseconds and seconds aren't going to be used very much. However, I disagree with the assertion given in the proposal that every developer knows that you can multiply or divide by 1000 to get from one to the other. I'd say that in fact many developers don't have the SI prefixes and their magnitudes at their fingertips, and Temporal should be accessible to those developers too. |
then for this group of develpers, how could they have Temporal at their fingertips? If they do not know the relationship between seconds, milliseconds, microseconds, and nanoseconds, how could you expect they will call the right methods? The very same developer could call the microseconds one while they should call the nanoseconds one, right? and providing 4 will make their life even harder than only provideing then 1 or 2 since now they have to learn the differences between these four SI prefixes and their magnitudes which are not in their fingertips... |
autocomplete can teach them of the presence of different methods, and grow their understanding, quite successfully - in that respect, more methods is an education improvement. |
They have microseconds from a data source, or need microseconds to put into another API, so they call the method that has "microseconds" in its name. That's how. |
Meeting 2024-05-23: At the upcoming June 2024 TC39 plenary meeting, we'll propose removing the following 6 functions for the seconds and microseconds units:
For decades, milliseconds has been (thanks to But we believe that the other two are less important, so can be omitted to save space. (From history: microseconds was only added to these APIs because it was weird to have 3 out of 4 units present, so if we remove seconds then removing microseconds too is a no-brainer.) |
Are there ever any edge cases where ms * 1e3 won't be microseconds, or ms / 1e3 won't be seconds? |
Well, in the current API
The user would need to |
Replace 1e3 with 1000n where appropriate :-) As long as this is the case, it's unfortunate to make users learn more unit math, but it seems "fine". |
The current IOW current Edited: Fixed typo in |
These replacement examples seem like pretty obviously massive ergonomic losses, but at least everyone can duplicate the same SO snippet or LLM output, so that instead of paying in engine size, we pay in "tons of websites' JS" size. |
Whoops, I forgot that we changed rounding for epoch* getters to be towards the beginning of time, not towards 1970-01-01.
Fortunately, microseconds turns out to be rarely-used unit in real-world ECMAScript programs, relative to milliseconds and seconds. Nanoseconds is probably used least of all, but with Temporal's smallest unit being nanoseconds, it's sure to get more use. That leaves microseconds. Originally we weren't even going to add microseconds-related methods on Instant and ZDT because we didn't think they'd get enough use. But we decided to go for consistency and add it. But in a world of size constraints, cutting microseconds seems like a pretty obvious choice. All this is to say that IMO it's OK if microseconds requires some weird math. And if the world changes and suddenly everyone loves microseconds, then it'd be easy to add these methods back in a later proposal. TL;DR - microseconds being difficult seems OK, IMO. |
There is an implementation of this, including documentation of the workarounds, here. |
Closes: #2849 Co-Authored-By: Richard Gibson <richard.gibson@gmail.com>
Closes: #2849 Co-Authored-By: Richard Gibson <richard.gibson@gmail.com>
Temporal.Instant
contains staticfromEpochSeconds
,fromEpochMilliseconds
,fromEpochMicroseconds
, andfromEpochNanoseconds
methods to construct new instances (the latter of which duplicates the constructor's functionality), as well asepochSeconds
,epochMilliseconds
,epochMicroseconds
, andepochNanoseconds
getters. The same four getters are also present onTemporal.ZonedDateTime
. Are all these needed?This issue is adapted from suggestions made by @FrankYFTang to help reduce the size of Temporal to address concerns from implementers (especially Android and Apple Watch) that Temporal's install size and/or in-memory size is too large.
For context, here's a quick explanation of why each of these methods exist. Some of them are definitely higher priority than others. The higher-priority ones are noted in the first list:
Temporal.Instant ( _epochNanoseconds_ )
- obviously required because it's a constructorTemporal.ZonedDateTime ( _epochNanoseconds_, _timeZoneLike_ [ , _calendarLike_ ] )
- obviously required because it's a constructorget Temporal.Instant.prototype.epochMilliseconds
- this is required for interop with legacyDate
, i.e.new Date(instant.epochMilliseconds)
get Temporal.ZonedDateTime.prototype.epochMilliseconds
- allows converting between ZDT andDate
without having to convert to aTemporal.Instant
first.Temporal.Instant.fromEpochMilliseconds ( _epochMilliseconds_ )
- this is used forDate
interop in the other direction: when you have a milliseconds value you got from aDate
and want to turn it into aTemporal.Instant
without having to convert it into aDate
first so that you can callDate.p.toTemporalInstant()
.get Temporal.Instant.prototype.epochNanoseconds
- this exposes theBigInt
value of the instant in its full resolutionTemporal.Instant.fromEpochNanoseconds ( _epochNanoseconds_ )
- Across Temporal, we try to train users to avoid constructors and instead usefrom
methods becausefrom
methods accept a wider set of types and are more likely for the user to be successful. This method on Instant, although it duplicates the constructor, helps reinforce this pattern, and aligns with the other common way to construct an instant which isfromEpochMilliseconds
.The methods below for seconds and microseconds are admittedly lower priority, because they're not required for
Date
interop nor are required for full-resolution nanoseconds. We could consider deferring them for a few years until the added size of a few methods is less of a big deal for Android and Apple Watch:Temporal.Instant.fromEpochMicroseconds ( _epochMicroseconds_ )
Temporal.Instant.fromEpochSeconds ( _epochSeconds_ )
get Temporal.Instant.prototype.epochMicroseconds
get Temporal.Instant.prototype.epochSeconds
get Temporal.ZonedDateTime.prototype.epochMicroseconds
get Temporal.ZonedDateTime.prototype.epochSeconds
The text was updated successfully, but these errors were encountered: