Skip to content

Commit 6cfab10

Browse files
authored
Centralize nodejs performance polyfill. NFC (#22590)
1 parent 893e0a9 commit 6cfab10

File tree

8 files changed

+22
-26
lines changed

8 files changed

+22
-26
lines changed

src/library.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,13 +1429,6 @@ addToLibrary({
14291429
emscripten_random: () => Math.random(),
14301430

14311431
emscripten_get_now: `;
1432-
#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 160000
1433-
// The performance global was added to node in v16.0.0:
1434-
// https://nodejs.org/api/globals.html#performance
1435-
if (ENVIRONMENT_IS_NODE) {
1436-
global.performance = require('perf_hooks').performance;
1437-
}
1438-
#endif
14391432
#if PTHREADS && !AUDIO_WORKLET
14401433
// Pthreads need their clocks synchronized to the execution of the main
14411434
// thread, so, when using them, make sure to adjust all timings to the

src/preamble.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
// An online HTML version (which may be of a different version of Emscripten)
1515
// is up at http://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html
1616

17-
#if PTHREADS
18-
#include "runtime_pthread.js"
19-
#endif
20-
2117
#if RELOCATABLE
2218
{{{ makeModuleReceiveWithVar('dynamicLibraries', undefined, '[]', true) }}}
2319
#endif
@@ -152,6 +148,10 @@ var HEAP_DATA_VIEW;
152148

153149
#include "runtime_shared.js"
154150

151+
#if PTHREADS
152+
#include "runtime_pthread.js"
153+
#endif
154+
155155
#if ASSERTIONS
156156
assert(!Module['STACK_SIZE'], 'STACK_SIZE can no longer be set at runtime. Use -sSTACK_SIZE at link time')
157157
#endif

src/preamble_minimal.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
#include "runtime_asan.js"
1313
#endif
1414

15-
#if PTHREADS
16-
#include "runtime_pthread.js"
17-
#endif
18-
1915
#if ASSERTIONS
2016
/** @type {function(*, string=)} */
2117
function assert(condition, text) {
@@ -60,6 +56,10 @@ var HEAP8, HEAP16, HEAP32, HEAPU8, HEAPU16, HEAPU32, HEAPF32, HEAPF64,
6056

6157
#include "runtime_shared.js"
6258

59+
#if PTHREADS
60+
#include "runtime_pthread.js"
61+
#endif
62+
6363
#if IMPORTED_MEMORY
6464
#if PTHREADS
6565
if (!ENVIRONMENT_IS_PTHREAD) {

src/runtime_pthread.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ if (ENVIRONMENT_IS_PTHREAD) {
4040
#endif
4141
},
4242
postMessage: (msg) => parentPort.postMessage(msg),
43-
performance: global.performance || { now: Date.now },
4443
});
4544
}
4645
#endif // ENVIRONMENT_MAY_BE_NODE

src/runtime_shared.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,15 @@ var toIndexType = (function() {
6262
return (i) => bigintMemoryBounds ? BigInt(i) : i;
6363
})();
6464
#endif
65+
66+
#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 160000
67+
// The performance global was added to node in v16.0.0:
68+
// https://nodejs.org/api/globals.html#performance
69+
if (ENVIRONMENT_IS_NODE) {
70+
// This is needed for emscripten_get_now and for pthreads support which
71+
// depends on it for accurate timing.
72+
// Use `global` rather than `globalThis` here since older versions of node
73+
// don't have `globalThis`.
74+
global.performance ??= require('perf_hooks').performance;
75+
}
76+
#endif

src/shell.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,6 @@ if (!ENVIRONMENT_IS_AUDIO_WORKLET)
428428
}
429429

430430
#if ENVIRONMENT_MAY_BE_NODE && PTHREADS
431-
if (ENVIRONMENT_IS_NODE) {
432-
// Polyfill the performance object, which emscripten pthreads support
433-
// depends on for good timing.
434-
if (typeof performance == 'undefined') {
435-
global.performance = require('perf_hooks').performance;
436-
}
437-
}
438-
439431
// Set up the out() and err() hooks, which are how we can print to stdout or
440432
// stderr, respectively.
441433
// Normally just binding console.log/console.error here works fine, but
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4959
1+
4916
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10602
1+
10465

0 commit comments

Comments
 (0)