Skip to content

Commit 26a851a

Browse files
author
John Doe
committed
refactor: remove hasTimeorigin
1 parent e33cc3b commit 26a851a

File tree

3 files changed

+15
-63
lines changed

3 files changed

+15
-63
lines changed

packages/utils/src/lib/clock-epoch.ts

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ export type Microseconds = number;
55
export type Milliseconds = number;
66
export type EpochMilliseconds = number;
77

8-
const hasPerf = (): boolean =>
9-
typeof performance !== 'undefined' && typeof performance.now === 'function';
10-
const hasTimeOrigin = (): boolean =>
11-
hasPerf() &&
12-
typeof (performance as { timeOrigin: number | undefined }).timeOrigin ===
13-
'number';
14-
158
const msToUs = (ms: number): Microseconds => Math.round(ms * 1000);
169
const usToUs = (us: number): Microseconds => Math.round(us);
1710

@@ -26,11 +19,6 @@ export type EpochClockOptions = {
2619
tid?: number;
2720
};
2821

29-
type MaybePerformance = typeof performance & {
30-
timeOrigin?: number;
31-
now?: number;
32-
};
33-
3422
/**
3523
* Creates epoch-based clock utility.
3624
* Epoch time has been the time since January 1, 1970 (UNIX epoch).
@@ -41,27 +29,17 @@ export function epochClock(init: EpochClockOptions = {}) {
4129
const pid = init.pid ?? process.pid;
4230
const tid = init.tid ?? threadId;
4331

44-
const timeOriginMs = hasTimeOrigin()
45-
? (performance as MaybePerformance).timeOrigin
46-
: Date.now();
32+
const timeOriginMs = performance.timeOrigin;
4733

48-
const epochNowUs = (): Microseconds => {
49-
if (hasTimeOrigin()) {
50-
return msToUs(timeOriginMs + performance.now());
51-
}
52-
return msToUs(Date.now());
53-
};
34+
const epochNowUs = (): Microseconds =>
35+
msToUs(timeOriginMs + performance.now());
5436

5537
const fromEpochUs = usToUs;
5638

5739
const fromEpochMs = msToUs;
5840

59-
const fromPerfMs = (perfMs: Milliseconds): Microseconds => {
60-
if (hasTimeOrigin()) {
61-
return msToUs(timeOriginMs + perfMs);
62-
}
63-
return epochNowUs() - msToUs(performance.now() - perfMs);
64-
};
41+
const fromPerfMs = (perfMs: Milliseconds): Microseconds =>
42+
msToUs(timeOriginMs + perfMs);
6543

6644
const fromEntryStartTimeMs = fromPerfMs;
6745
const fromDateNowMs = fromEpochMs;
@@ -71,7 +49,6 @@ export function epochClock(init: EpochClockOptions = {}) {
7149
pid,
7250
tid,
7351

74-
hasTimeOrigin,
7552
epochNowUs,
7653
msToUs,
7754
usToUs,

packages/utils/src/lib/clock-epoch.unit.test.ts

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,46 +36,12 @@ describe('epochClock', () => {
3636
);
3737
});
3838

39-
it('should return undefined if performance.timeOrigin is NOT present', () => {
40-
vi.stubGlobal('performance', {
41-
...performance,
42-
timeOrigin: undefined,
43-
});
44-
const c = epochClock();
45-
expect(c.hasTimeOrigin()).toBe(false);
46-
});
47-
48-
it('should return timeorigin if performance.timeOrigin is present', () => {
49-
const c = epochClock();
50-
expect(c.hasTimeOrigin()).toBe(true);
51-
});
52-
5339
it('should support performance clock by default for epochNowUs', () => {
5440
const c = epochClock();
5541
expect(c.timeOriginMs).toBe(500_000);
5642
expect(c.epochNowUs()).toBe(1_000_000_000); // timeOrigin + (Date.now() - timeOrigin) = Date.now()
5743
});
5844

59-
it('should fallback to Date clock by if performance clock is not given in epochNowUs', () => {
60-
vi.stubGlobal('performance', {
61-
...performance,
62-
timeOrigin: undefined,
63-
});
64-
const c = epochClock();
65-
expect(c.timeOriginMs).toBe(1_000_000);
66-
expect(c.epochNowUs()).toBe(1_000_000_000); // Date.now() * 1000 when performance unavailable
67-
});
68-
69-
it('should fallback to Date clock by if performance clock is NOT given', () => {
70-
vi.stubGlobal('performance', {
71-
...performance,
72-
timeOrigin: undefined,
73-
});
74-
const c = epochClock();
75-
expect(c.timeOriginMs).toBe(1_000_000);
76-
expect(c.fromPerfMs(100)).toBe(500_100_000); // epochNowUs() - msToUs(performance.now() - perfMs)
77-
});
78-
7945
it.each([
8046
[1_000_000_000, 1_000_000_000],
8147
[1_001_000_000, 1_001_000_000],
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import process from 'node:process';
2+
import { threadId } from 'node:worker_threads';
23
import { afterEach, beforeEach, vi } from 'vitest';
34

45
export const MOCK_PID = 10_001;
6+
export const MOCK_TID = 1;
7+
8+
// Mock immediately when the setup file loads for default exports using process.pid or threadId
9+
const processMock = vi.spyOn(process, 'pid', 'get').mockReturnValue(MOCK_PID);
10+
const threadIdMock = vi
11+
.spyOn({ threadId }, 'threadId', 'get')
12+
.mockReturnValue(MOCK_TID);
513

6-
const processMock = vi.spyOn(process, 'pid', 'get');
714
beforeEach(() => {
815
processMock.mockReturnValue(MOCK_PID);
16+
threadIdMock.mockReturnValue(MOCK_TID);
917
});
1018

1119
afterEach(() => {
1220
processMock.mockClear();
21+
threadIdMock.mockClear();
1322
});

0 commit comments

Comments
 (0)