Skip to content

Commit 8fdf8bf

Browse files
committed
perf_hooks: make performance a global
Signed-off-by: James M Snell <[email protected]>
1 parent 30fe4ed commit 8fdf8bf

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -331,5 +331,6 @@ module.exports = {
331331
globalThis: 'readable',
332332
btoa: 'readable',
333333
atob: 'readable',
334+
performance: 'readable',
334335
},
335336
};

doc/api/globals.md

+5
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ The `MessagePort` class. See [`MessagePort`][] for more details.
279279

280280
This variable may appear to be global but is not. See [`module`][].
281281

282+
## `performance`
283+
284+
The [`perf_hooks.performance`][] object.
285+
282286
## `process`
283287
<!-- YAML
284288
added: v0.1.7
@@ -428,6 +432,7 @@ The object that acts as the namespace for all W3C
428432
[`console`]: console.md
429433
[`exports`]: modules.md#modules_exports
430434
[`module`]: modules.md#modules_module
435+
[`perf_hooks.performance`]: perf_hooks.md#perf_hooks_perf_hooks_performance
431436
[`process.nextTick()`]: process.md#process_process_nexttick_callback_args
432437
[`process` object]: process.md#process_process
433438
[`require()`]: modules.md#modules_require_id

lib/internal/bootstrap/node.js

+23
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ if (!config.noBrowserGlobals) {
236236

237237
defineOperation(global, 'queueMicrotask', queueMicrotask);
238238

239+
defineLazyGlobal(global, 'performance', () => {
240+
const { performance } = require('perf_hooks');
241+
return performance;
242+
});
243+
239244
// Non-standard extensions:
240245
defineOperation(global, 'clearImmediate', timers.clearImmediate);
241246
defineOperation(global, 'setImmediate', timers.setImmediate);
@@ -481,3 +486,21 @@ function defineOperation(target, name, method) {
481486
value: method
482487
});
483488
}
489+
490+
function defineLazyGlobal(target, name, loader) {
491+
let value;
492+
let overridden = false;
493+
ObjectDefineProperty(target, name, {
494+
enumerable: true,
495+
configurable: true,
496+
get() {
497+
if (value === undefined && !overridden)
498+
value = loader();
499+
return value;
500+
},
501+
set(val) {
502+
value = val;
503+
overridden = true;
504+
}
505+
});
506+
}

test/common/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ let knownGlobals = [
274274
queueMicrotask,
275275
];
276276

277+
if (globalThis.performance)
278+
knownGlobals.push(globalThis.performance);
279+
277280
// TODO(@jasnell): This check can be temporary. AbortController is
278281
// not currently supported in either Node.js 12 or 10, making it
279282
// difficult to run tests comparitively on those versions. Once
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
/* eslint-disable no-global-assign */
3+
4+
require('../common');
5+
6+
const perf_hooks = require('perf_hooks');
7+
const {
8+
strictEqual
9+
} = require('assert');
10+
11+
strictEqual(globalThis.performance, perf_hooks.performance);
12+
performance = undefined;
13+
strictEqual(globalThis.performance, undefined);
14+
strictEqual(typeof perf_hooks.performance.now, 'function');

test/wpt/test-hr-time.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ runner.setInitScript(`
99
const { Blob } = require('buffer');
1010
global.Blob = Blob;
1111
12-
const { performance, PerformanceObserver } = require('perf_hooks');
13-
global.performance = performance;
12+
const { PerformanceObserver } = require('perf_hooks');
1413
global.PerformanceObserver = PerformanceObserver;
1514
`);
1615

0 commit comments

Comments
 (0)