diff --git a/src/library.js b/src/library.js index 09b4f4fb607d1..991fa7952c1ca 100644 --- a/src/library.js +++ b/src/library.js @@ -1429,13 +1429,6 @@ addToLibrary({ emscripten_random: () => Math.random(), emscripten_get_now: `; -#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 160000 - // The performance global was added to node in v16.0.0: - // https://nodejs.org/api/globals.html#performance - if (ENVIRONMENT_IS_NODE) { - global.performance = require('perf_hooks').performance; - } -#endif #if PTHREADS && !AUDIO_WORKLET // Pthreads need their clocks synchronized to the execution of the main // thread, so, when using them, make sure to adjust all timings to the diff --git a/src/preamble.js b/src/preamble.js index 61d0858a9e657..40dd0f3747f78 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -14,10 +14,6 @@ // An online HTML version (which may be of a different version of Emscripten) // is up at http://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html -#if PTHREADS -#include "runtime_pthread.js" -#endif - #if RELOCATABLE {{{ makeModuleReceiveWithVar('dynamicLibraries', undefined, '[]', true) }}} #endif @@ -152,6 +148,10 @@ var HEAP_DATA_VIEW; #include "runtime_shared.js" +#if PTHREADS +#include "runtime_pthread.js" +#endif + #if ASSERTIONS assert(!Module['STACK_SIZE'], 'STACK_SIZE can no longer be set at runtime. Use -sSTACK_SIZE at link time') #endif diff --git a/src/preamble_minimal.js b/src/preamble_minimal.js index f110870d2b77e..42805b09e35dc 100644 --- a/src/preamble_minimal.js +++ b/src/preamble_minimal.js @@ -12,10 +12,6 @@ #include "runtime_asan.js" #endif -#if PTHREADS -#include "runtime_pthread.js" -#endif - #if ASSERTIONS /** @type {function(*, string=)} */ function assert(condition, text) { @@ -60,6 +56,10 @@ var HEAP8, HEAP16, HEAP32, HEAPU8, HEAPU16, HEAPU32, HEAPF32, HEAPF64, #include "runtime_shared.js" +#if PTHREADS +#include "runtime_pthread.js" +#endif + #if IMPORTED_MEMORY #if PTHREADS if (!ENVIRONMENT_IS_PTHREAD) { diff --git a/src/runtime_pthread.js b/src/runtime_pthread.js index 92c97f5dbceaf..6444102a16abf 100644 --- a/src/runtime_pthread.js +++ b/src/runtime_pthread.js @@ -40,7 +40,6 @@ if (ENVIRONMENT_IS_PTHREAD) { #endif }, postMessage: (msg) => parentPort.postMessage(msg), - performance: global.performance || { now: Date.now }, }); } #endif // ENVIRONMENT_MAY_BE_NODE diff --git a/src/runtime_shared.js b/src/runtime_shared.js index 7c23e943e64b8..5606ac4c91162 100644 --- a/src/runtime_shared.js +++ b/src/runtime_shared.js @@ -62,3 +62,15 @@ var toIndexType = (function() { return (i) => bigintMemoryBounds ? BigInt(i) : i; })(); #endif + +#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 160000 +// The performance global was added to node in v16.0.0: +// https://nodejs.org/api/globals.html#performance +if (ENVIRONMENT_IS_NODE) { + // This is needed for emscripten_get_now and for pthreads support which + // depends on it for accurate timing. + // Use `global` rather than `globalThis` here since older versions of node + // don't have `globalThis`. + global.performance ??= require('perf_hooks').performance; +} +#endif diff --git a/src/shell.js b/src/shell.js index d2334a83dac2f..6183dc91dcaf4 100644 --- a/src/shell.js +++ b/src/shell.js @@ -428,14 +428,6 @@ if (!ENVIRONMENT_IS_AUDIO_WORKLET) } #if ENVIRONMENT_MAY_BE_NODE && PTHREADS -if (ENVIRONMENT_IS_NODE) { - // Polyfill the performance object, which emscripten pthreads support - // depends on for good timing. - if (typeof performance == 'undefined') { - global.performance = require('perf_hooks').performance; - } -} - // Set up the out() and err() hooks, which are how we can print to stdout or // stderr, respectively. // Normally just binding console.log/console.error here works fine, but diff --git a/test/other/codesize/test_codesize_minimal_pthreads.gzsize b/test/other/codesize/test_codesize_minimal_pthreads.gzsize index f4833d49363ab..69a8eaf5a510b 100644 --- a/test/other/codesize/test_codesize_minimal_pthreads.gzsize +++ b/test/other/codesize/test_codesize_minimal_pthreads.gzsize @@ -1 +1 @@ -4959 +4916 diff --git a/test/other/codesize/test_codesize_minimal_pthreads.jssize b/test/other/codesize/test_codesize_minimal_pthreads.jssize index 4cdee9867c3b4..6cc48c769c974 100644 --- a/test/other/codesize/test_codesize_minimal_pthreads.jssize +++ b/test/other/codesize/test_codesize_minimal_pthreads.jssize @@ -1 +1 @@ -10602 +10465