Skip to content

Commit 1da28fe

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

File tree

7 files changed

+53
-2
lines changed

7 files changed

+53
-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

+4
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ if (global.gc) {
287287
knownGlobals.push(global.gc);
288288
}
289289

290+
if (global.performance) {
291+
knownGlobals.push(global.performance);
292+
}
293+
290294
function allowGlobals(...allowlist) {
291295
knownGlobals = knownGlobals.concat(allowlist);
292296
}

test/parallel/test-global.js

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ builtinModules.forEach((moduleName) => {
4747
'clearImmediate',
4848
'clearInterval',
4949
'clearTimeout',
50+
'performance',
5051
'setImmediate',
5152
'setInterval',
5253
'setTimeout'
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
const perf = performance;
12+
strictEqual(globalThis.performance, perf_hooks.performance);
13+
performance = undefined;
14+
strictEqual(globalThis.performance, undefined);
15+
strictEqual(typeof perf_hooks.performance.now, 'function');
16+
17+
// Restore the value of performance for the known globals check
18+
performance = perf;

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)