Skip to content

Commit

Permalink
perf_hooks: make performance a global
Browse files Browse the repository at this point in the history
Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #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>
  • Loading branch information
jasnell committed Apr 1, 2021
1 parent e0eb515 commit f0bf373
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 2 deletions.
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', () => {
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

0 comments on commit f0bf373

Please sign in to comment.