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>
  • Loading branch information
jasnell committed Mar 29, 2021
1 parent d1e2184 commit e34c9b0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 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;
}
});
}
1 change: 1 addition & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ let knownGlobals = [
clearInterval,
clearTimeout,
global,
performance,
setImmediate,
setInterval,
setTimeout,
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');

0 comments on commit e34c9b0

Please sign in to comment.