jerryscript-project / jerryscript

Ultra-lightweight JavaScript engine for the Internet of Things.
https://jerryscript.net
Apache License 2.0
6.91k stars 669 forks source link

temporal.now require ` nanosecond accuracy` #4975

Open lygstate opened 2 years ago

lygstate commented 2 years ago

According to https://tc39.es/proposal-temporal/docs/now.html#instant,

The Temporal.Now.instant() are improved the accuracy over Date.now from millisecond to nanosecond, that's need tthe jerryscript's port API to support nanosecond precision

dbatyai commented 2 years ago

According to the proposal:

Returns: a Temporal.Instant object representing the current system time.

I'm going to refer to my previous comment on this topic.

Is there actually a platform in existence that supports true nanosecond clock resolution for the system clock? So far I have yet to see any, but if I'm wrong then please correct me.

As such, even if the proposed Temporal object is able to store a time value in nanosecond precision does not mean that underlying hardware has sufficiently high precision timers, or that operating systems provide a way to query timers in that precision.

I think the way the proposal should be interpreted is the following: Take the current system time using the highest precision clock available, and scale it to nanoseconds. For this purpose the current port function is already more than sufficient.

lygstate commented 2 years ago

According to the proposal:

Returns: a Temporal.Instant object representing the current system time.

I'm going to refer to my previous comment on this topic.

Is there actually a platform in existence that supports true nanosecond clock resolution for the system clock? So far I have yet to see any, but if I'm wrong then please correct me.

Hi, on windows, the system clock is 100ns(refer to https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimeasfiletime), on most posix platform, it's nanosecond, please refer to clock_gettime

As such, even if the proposed Temporal object is able to store a time value in nanosecond precision does not mean that underlying hardware has sufficiently high precision timers, or that operating systems provide a way to query timers in that precision.

I think the way the proposal should be interpreted is the following: Take the current system time using the highest precision clock available, and scale it to nanoseconds. For this purpose the current port function is already more than sufficient.

dbatyai commented 2 years ago

Well, in my experience the tv_nsec member never actually contains nanosecond precision values, it's always a multiple of 1000. I'm also rather skeptical about the 100ns resolution in Windows, I would say it's also pretty likely that it's not fully utilized.

However you are correct that at least there are possibilities to query in (close to) nanosecond precision. I guess it would probably make sense to update the port function to resemble clock_gettime, just in case, even if we can't fully utilize it yet.