perf_hooks: make performance a global

Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: https://github.com/nodejs/node/pull/37970
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
This commit is contained in:
James M Snell
2021-03-29 14:09:59 -07:00
parent e0eb515afc
commit f0bf373176
7 changed files with 53 additions and 2 deletions

View File

@@ -331,5 +331,6 @@ module.exports = {
globalThis: 'readable',
btoa: 'readable',
atob: 'readable',
performance: 'readable',
},
};

View File

@@ -279,6 +279,10 @@ The `MessagePort` class. See [`MessagePort`][] for more details.
This variable may appear to be global but is not. See [`module`][].
## `performance`
The [`perf_hooks.performance`][] object.
## `process`
<!-- YAML
added: v0.1.7
@@ -428,6 +432,7 @@ The object that acts as the namespace for all W3C
[`console`]: console.md
[`exports`]: modules.md#modules_exports
[`module`]: modules.md#modules_module
[`perf_hooks.performance`]: perf_hooks.md#perf_hooks_perf_hooks_performance
[`process.nextTick()`]: process.md#process_process_nexttick_callback_args
[`process` object]: process.md#process_process
[`require()`]: modules.md#modules_require_id

View File

@@ -236,6 +236,11 @@ if (!config.noBrowserGlobals) {
defineOperation(global, 'queueMicrotask', queueMicrotask);
defineLazyGlobal(global, 'performance', () => {
const { performance } = require('perf_hooks');
return performance;
});
// Non-standard extensions:
defineOperation(global, 'clearImmediate', timers.clearImmediate);
defineOperation(global, 'setImmediate', timers.setImmediate);
@@ -481,3 +486,21 @@ function defineOperation(target, name, method) {
value: method
});
}
function defineLazyGlobal(target, name, loader) {
let value;
let overridden = false;
ObjectDefineProperty(target, name, {
enumerable: true,
configurable: true,
get() {
if (value === undefined && !overridden)
value = loader();
return value;
},
set(val) {
value = val;
overridden = true;
}
});
}

View File

@@ -287,6 +287,10 @@ if (global.gc) {
knownGlobals.push(global.gc);
}
if (global.performance) {
knownGlobals.push(global.performance);
}
function allowGlobals(...allowlist) {
knownGlobals = knownGlobals.concat(allowlist);
}

View File

@@ -47,6 +47,7 @@ builtinModules.forEach((moduleName) => {
'clearImmediate',
'clearInterval',
'clearTimeout',
'performance',
'setImmediate',
'setInterval',
'setTimeout'

View File

@@ -0,0 +1,18 @@
'use strict';
/* eslint-disable no-global-assign */
require('../common');
const perf_hooks = require('perf_hooks');
const {
strictEqual
} = require('assert');
const perf = performance;
strictEqual(globalThis.performance, perf_hooks.performance);
performance = undefined;
strictEqual(globalThis.performance, undefined);
strictEqual(typeof perf_hooks.performance.now, 'function');
// Restore the value of performance for the known globals check
performance = perf;

View File

@@ -9,8 +9,7 @@ runner.setInitScript(`
const { Blob } = require('buffer');
global.Blob = Blob;
const { performance, PerformanceObserver } = require('perf_hooks');
global.performance = performance;
const { PerformanceObserver } = require('perf_hooks');
global.PerformanceObserver = PerformanceObserver;
`);