- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.7k
ref(utils): small gains attempt #4324
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
          
     Closed
      
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            3 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -16,7 +16,7 @@ interface TimestampSource { | |
| * is more obvious to explain "why does my span have negative duration" than "why my spans have zero duration". | ||
| */ | ||
| const dateTimestampSource: TimestampSource = { | ||
| nowSeconds: () => Date.now() / 1000, | ||
| nowSeconds: () => msToSec(Date.now()), | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can prob just stop using the  diff --git a/packages/utils/src/time.ts b/packages/utils/src/time.ts
index 932fffc9..ca59433a 100644
--- a/packages/utils/src/time.ts
+++ b/packages/utils/src/time.ts
@@ -1,24 +1,6 @@
 import { getGlobalObject } from './global';
 import { dynamicRequire, isNodeEnv } from './node';
 
-/**
- * An object that can return the current timestamp in seconds since the UNIX epoch.
- */
-interface TimestampSource {
-  nowSeconds(): number;
-}
-
-/**
- * A TimestampSource implementation for environments that do not support the Performance Web API natively.
- *
- * Note that this TimestampSource does not use a monotonic clock. A call to `nowSeconds` may return a timestamp earlier
- * than a previously returned value. We do not try to emulate a monotonic behavior in order to facilitate debugging. It
- * is more obvious to explain "why does my span have negative duration" than "why my spans have zero duration".
- */
-const dateTimestampSource: TimestampSource = {
-  nowSeconds: () => Date.now() / 1000,
-};
-
 /**
  * A partial definition of the [Performance Web API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Performance}
  * for accessing a high resolution monotonic clock.
@@ -89,21 +71,14 @@ function getNodePerformance(): Performance | undefined {
 }
 
 /**
- * The Performance API implementation for the current platform, if available.
+ * Returns a timestamp in seconds since the UNIX epoch using the Date API.
  */
-const platformPerformance: Performance | undefined = isNodeEnv() ? getNodePerformance() : getBrowserPerformance();
-
-const timestampSource: TimestampSource =
-  platformPerformance === undefined
-    ? dateTimestampSource
-    : {
-        nowSeconds: () => (platformPerformance.timeOrigin + platformPerformance.now()) / 1000,
-      };
+export const dateTimestampInSeconds: () => number = () => Date.now() / 1000;
 
 /**
- * Returns a timestamp in seconds since the UNIX epoch using the Date API.
+ * The Performance API implementation for the current platform, if available.
  */
-export const dateTimestampInSeconds: () => number = dateTimestampSource.nowSeconds.bind(dateTimestampSource);
+const platformPerformance: Performance | undefined = isNodeEnv() ? getNodePerformance() : getBrowserPerformance();
 
 /**
  * Returns a timestamp in seconds since the UNIX epoch using either the Performance or Date APIs, depending on the
@@ -116,7 +91,10 @@ export const dateTimestampInSeconds: () => number = dateTimestampSource.nowSecon
  * skew can grow to arbitrary amounts like days, weeks or months.
  * See https://github.com/getsentry/sentry-javascript/issues/2590.
  */
-export const timestampInSeconds: () => number = timestampSource.nowSeconds.bind(timestampSource);
+export const timestampInSeconds =
+  platformPerformance === undefined
+    ? dateTimestampInSeconds
+    : () => (platformPerformance.timeOrigin + platformPerformance.now()) / 1000;
 
 // Re-exported with an old name for backwards-compatibility.
 export const timestampWithMs = timestampInSeconds; | ||
| }; | ||
|  | ||
| /** | ||
|  | @@ -97,7 +97,7 @@ const timestampSource: TimestampSource = | |
| platformPerformance === undefined | ||
| ? dateTimestampSource | ||
| : { | ||
| nowSeconds: () => (platformPerformance.timeOrigin + platformPerformance.now()) / 1000, | ||
| nowSeconds: () => msToSec(platformPerformance.timeOrigin + platformPerformance.now()), | ||
| }; | ||
|  | ||
| /** | ||
|  | @@ -183,3 +183,19 @@ export const browserPerformanceTimeOrigin = ((): number | undefined => { | |
| _browserPerformanceTimeOriginMode = 'dateNow'; | ||
| return dateNow; | ||
| })(); | ||
|  | ||
| /** | ||
| * Converts from milliseconds to seconds | ||
| * @param time time in ms | ||
| */ | ||
| export function msToSec(time: number): number { | ||
| return time / 1000; | ||
| } | ||
|  | ||
| /** | ||
| * Converts from seconds to milliseconds | ||
| * @param time time in seconds | ||
| */ | ||
| export function secToMs(time: number): number { | ||
| return time * 1000; | ||
| } | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
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 wonder if there is performance implications from un-unrolling the loop here, esp since we use this heavily for tracing
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.
@AbhiPrasad let me benchmark this
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.
Yup, about 5% drop https://jsbench.me/pmkx990pc4/1, I think I'll just drop this PR, negligible bundle size wins.