Skip to content
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

perf_hooks: make performance a global #37970

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,5 +331,6 @@ module.exports = {
globalThis: 'readable',
btoa: 'readable',
atob: 'readable',
performance: 'readable',
},
};
5 changes: 5 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ if (!config.noBrowserGlobals) {

defineOperation(global, 'queueMicrotask', queueMicrotask);

defineLazyGlobal(global, 'performance', () => {
targos marked this conversation as resolved.
Show resolved Hide resolved
const { performance } = require('perf_hooks');
return performance;
});

// Non-standard extensions:
defineOperation(global, 'clearImmediate', timers.clearImmediate);
defineOperation(global, 'setImmediate', timers.setImmediate);
Expand Down Expand Up @@ -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;
}
});
}
4 changes: 4 additions & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ builtinModules.forEach((moduleName) => {
'clearImmediate',
'clearInterval',
'clearTimeout',
'performance',
'setImmediate',
'setInterval',
'setTimeout'
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-performance-global.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 1 addition & 2 deletions test/wpt/test-hr-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
`);

Expand Down