-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
ref(utils): Clean up timestamp calculation code #10069
Conversation
@@ -148,7 +90,7 @@ export const browserPerformanceTimeOrigin = ((): number | undefined => { | |||
// performance.timing.navigationStart, which results in poor results in performance data. We only treat time origin | |||
// data as reliable if they are within a reasonable threshold of the current time. | |||
|
|||
const { performance } = WINDOW; | |||
const { performance } = GLOBAL_OBJ as typeof GLOBAL_OBJ & Window; | |||
if (!performance || !performance.now) { | |||
_browserPerformanceTimeOriginMode = 'none'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally what we do with browserPerformanceTimeOrigin
is to make this timeOrigin
calculation more generic for browser/node, I generally think it uses a good heuristic to determine timeOrigin
reliability.
size-limit report 📦
|
@@ -148,7 +90,7 @@ export const browserPerformanceTimeOrigin = ((): number | undefined => { | |||
// performance.timing.navigationStart, which results in poor results in performance data. We only treat time origin | |||
// data as reliable if they are within a reasonable threshold of the current time. | |||
|
|||
const { performance } = WINDOW; | |||
const { performance } = GLOBAL_OBJ as typeof GLOBAL_OBJ & Window; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this a new type instead of this type cast?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine to keep esp because it's only being used here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
This PR removes the usage of `dynamicRequire` from `packages/utils/src/time.ts` and cleans up our timestamp code to be simpler and reduce bundle size. Removing `dynamicRequire` means that we no longer rely on `perf_hooks` for the `performance` API, but instead try to grab it from `globalThis.performance`. `performance` was added to the global in Node 16, which means we'll fallback to using `Date.now()` in Node 8, 10, 12, 14 (and in v8 just Node 14). I think that is an acceptable tradeoff, we just reduce accuracy for those versions. This does not refactor `browserPerformanceTimeOrigin` code at the bottom of the file, I want to come back and touch that in a follow up PR to reduce the amount of changes made here. I would appreciate reviews/opinions on this, I'm not 100% confident in the changes.
ref #10046
This PR removes the usage of
dynamicRequire
frompackages/utils/src/time.ts
and cleans up our timestamp code to be simpler and reduce bundle size. RemovingdynamicRequire
means that we no longer rely onperf_hooks
for theperformance
API, but instead try to grab it fromglobalThis.performance
.performance
was added to the global in Node 16, which means we'll fallback to usingDate.now()
in Node 8, 10, 12, 14 (and in v8 just Node 14). I think that is an acceptable tradeoff, we just reduce accuracy for those versions.This does not refactor
browserPerformanceTimeOrigin
code at the bottom of the file, I want to come back and touch that in a follow up PR to reduce the amount of changes made here.I would appreciate reviews/opinions on this, I'm not 100% confident in the changes.