Skip to content

Commit 4b1c0f9

Browse files
authored
Optimize code size by using ?. notation (#21708)
And also using `!=` when we know the LHS are RHS are both string, specifically `typeof xx` on the left and a string literal on the right.
1 parent c8da47e commit 4b1c0f9

File tree

80 files changed

+87
-87
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+87
-87
lines changed

src/embind/emval.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ var LibraryEmVal = {
315315
#if MIN_CHROME_VERSION < 49 || MIN_FIREFOX_VERSION < 42 || MIN_SAFARI_VERSION < 100101
316316
$reflectConstruct: null,
317317
$reflectConstruct__postset: `
318-
if (typeof Reflect !== 'undefined') {
318+
if (typeof Reflect != 'undefined') {
319319
reflectConstruct = Reflect.construct;
320320
} else {
321321
reflectConstruct = function(target, args) {

src/library_atomic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ addToLibrary({
2222
// https://github.com/tc39/proposal-atomics-wait-async/blob/master/PROPOSAL.md
2323
// This polyfill performs polling with setTimeout() to observe a change in the
2424
// target memory location.
25-
$polyfillWaitAsync__postset: `if (!Atomics.waitAsync || (typeof navigator !== 'undefined' && navigator.userAgent && jstoi_q((navigator.userAgent.match(/Chrom(e|ium)\\/([0-9]+)\\./)||[])[2]) < 91)) {
25+
$polyfillWaitAsync__postset: `if (!Atomics.waitAsync || (typeof navigator != 'undefined' && navigator.userAgent && jstoi_q((navigator.userAgent.match(/Chrom(e|ium)\\/([0-9]+)\\./)||[])[2]) < 91)) {
2626
let __Atomics_waitAsyncAddresses = [/*[i32a, index, value, maxWaitMilliseconds, promiseResolve]*/];
2727
function __Atomics_pollWaitAsyncAddresses() {
2828
let now = performance.now();

src/library_promise.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ addToLibrary({
101101
// MEMORY64 mode when they are later converted to void* rejection
102102
// values.
103103
#if MEMORY64
104-
if (typeof e !== 'bigint') {
104+
if (typeof e != 'bigint') {
105105
throw 0n;
106106
}
107107
#else
108-
if (typeof e !== 'number') {
108+
if (typeof e != 'number') {
109109
throw 0;
110110
}
111111
#endif
@@ -224,7 +224,7 @@ addToLibrary({
224224
dbg(`emscripten_promise_any: ${promises}`);
225225
#endif
226226
#if ASSERTIONS
227-
assert(typeof Promise.any !== 'undefined', "Promise.any does not exist");
227+
assert(typeof Promise.any != 'undefined', "Promise.any does not exist");
228228
#endif
229229
var id = promiseMap.allocate({
230230
promise: Promise.any(promises).catch((err) => {

src/library_wasi.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ var WasiLibrary = {
207207
if (curr < 0) return -1;
208208
ret += curr;
209209
if (curr < len) break; // nothing more to read
210-
if (typeof offset !== 'undefined') {
210+
if (typeof offset != 'undefined') {
211211
offset += curr;
212212
}
213213
}
@@ -223,7 +223,7 @@ var WasiLibrary = {
223223
var curr = FS.write(stream, HEAP8, ptr, len, offset);
224224
if (curr < 0) return -1;
225225
ret += curr;
226-
if (typeof offset !== 'undefined') {
226+
if (typeof offset != 'undefined') {
227227
offset += curr;
228228
}
229229
}

src/runtime_debug.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function isExportedByForceFilesystem(name) {
4141
}
4242

4343
function missingGlobal(sym, msg) {
44-
if (typeof globalThis !== 'undefined') {
44+
if (typeof globalThis != 'undefined') {
4545
Object.defineProperty(globalThis, sym, {
4646
configurable: true,
4747
get() {
@@ -56,7 +56,7 @@ missingGlobal('buffer', 'Please use HEAP8.buffer or wasmMemory.buffer');
5656
missingGlobal('asm', 'Please use wasmExports instead');
5757

5858
function missingLibrarySymbol(sym) {
59-
if (typeof globalThis !== 'undefined' && !Object.getOwnPropertyDescriptor(globalThis, sym)) {
59+
if (typeof globalThis != 'undefined' && !Object.getOwnPropertyDescriptor(globalThis, sym)) {
6060
Object.defineProperty(globalThis, sym, {
6161
configurable: true,
6262
get() {

src/shell.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ var ENVIRONMENT_IS_WASM_WORKER = Module['$ww'];
146146
#if SHARED_MEMORY && !MODULARIZE
147147
// In MODULARIZE mode _scriptDir needs to be captured already at the very top of the page immediately when the page is parsed, so it is generated there
148148
// before the page load. In non-MODULARIZE modes generate it here.
149-
var _scriptDir = (typeof document != 'undefined' && document.currentScript) ? document.currentScript.src : undefined;
149+
var _scriptDir = (typeof document != 'undefined') ? document.currentScript?.src : undefined;
150150

151151
if (ENVIRONMENT_IS_WORKER) {
152152
_scriptDir = self.location.href;

src/shell_minimal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ function ready() {
143143
#if !MODULARIZE
144144
// In MODULARIZE mode _scriptDir needs to be captured already at the very top of the page immediately when the page is parsed, so it is generated there
145145
// before the page load. In non-MODULARIZE modes generate it here.
146-
var _scriptDir = (typeof document != 'undefined' && document.currentScript) ? document.currentScript.src : undefined;
146+
var _scriptDir = (typeof document != 'undefined') ? document.currentScript?.src : undefined;
147147
#endif
148148

149149
// MINIMAL_RUNTIME does not support --proxy-to-worker option, so Worker and Pthread environments
@@ -171,7 +171,7 @@ if (ENVIRONMENT_IS_NODE) {
171171
}
172172
#endif
173173

174-
var currentScriptUrl = typeof _scriptDir != 'undefined' ? _scriptDir : ((typeof document != 'undefined' && document.currentScript) ? document.currentScript.src : undefined);
174+
var currentScriptUrl = typeof _scriptDir != 'undefined' ? _scriptDir : ((typeof document != 'undefined') ? document.currentScript?.src : undefined);
175175
#endif // PTHREADS
176176

177177
#if !SINGLE_FILE

src/worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ if (ENVIRONMENT_IS_NODE) {
4545
// __filename is undefined in ES6 modules, and import.meta.url only in ES6
4646
// modules.
4747
#if EXPORT_ES6
48-
href: typeof __filename !== 'undefined' ? __filename : import.meta.url
48+
href: typeof __filename != 'undefined' ? __filename : import.meta.url
4949
#else
5050
href: __filename
5151
#endif

test/optimizer/applyImportAndExportNameChanges2-output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function abort(what) {
2525
throw what;
2626
}
2727

28-
var UTF8Decoder = typeof TextDecoder !== "undefined" ? new TextDecoder("utf8") : undefined;
28+
var UTF8Decoder = typeof TextDecoder != "undefined" ? new TextDecoder("utf8") : undefined;
2929

3030
function UTF8ArrayToString(u8Array, idx, maxBytesToRead) {
3131
var endIdx = idx + maxBytesToRead;

test/optimizer/applyImportAndExportNameChanges2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function ready() {
2121
function abort(what) {
2222
throw what
2323
}
24-
var UTF8Decoder = typeof TextDecoder !== "undefined" ? new TextDecoder("utf8") : undefined;
24+
var UTF8Decoder = typeof TextDecoder != "undefined" ? new TextDecoder("utf8") : undefined;
2525

2626
function UTF8ArrayToString(u8Array, idx, maxBytesToRead) {
2727
var endIdx = idx + maxBytesToRead;

0 commit comments

Comments
 (0)