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 <[email protected]>
  • Loading branch information
jasnell committed Mar 30, 2021
1 parent 30fe4ed commit 8fdf8bf
Show file tree
Hide file tree
Showing 6 changed files with 47 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;
}
});
}
3 changes: 3 additions & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ let knownGlobals = [
queueMicrotask,
];

if (globalThis.performance)
knownGlobals.push(globalThis.performance);

// TODO(@jasnell): This check can be temporary. AbortController is
// not currently supported in either Node.js 12 or 10, making it
// difficult to run tests comparitively on those versions. Once
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-performance-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
/* eslint-disable no-global-assign */

require('../common');

const perf_hooks = require('perf_hooks');
const {
strictEqual
} = require('assert');

strictEqual(globalThis.performance, perf_hooks.performance);
performance = undefined;
strictEqual(globalThis.performance, undefined);
strictEqual(typeof perf_hooks.performance.now, 'function');
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 8fdf8bf

Please sign in to comment.