Skip to content

Commit 4793205

Browse files
committed
Avoid unnecessary if of array.forEach in JS. NFC
In both these cases the code is smaller and simpler.
1 parent 10a4a9e commit 4793205

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

src/lib/libfs_shared.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ addToLibrary({
1919
if (typeof Browser != 'undefined') Browser.init();
2020
#endif
2121

22-
var handled = false;
23-
preloadPlugins.forEach((plugin) => {
24-
if (handled) return;
22+
for (var plugin of preloadPlugins) {
2523
if (plugin['canHandle'](fullname)) {
2624
plugin['handle'](byteArray, fullname, finish, onerror);
27-
handled = true;
25+
return true;
2826
}
29-
});
30-
return handled;
27+
}
28+
return false;
3129
},
3230
#endif
3331

src/lib/liblz4.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ addToLibrary({
3030
compressedData['cachedOffset'] + (i+1)*LZ4.CHUNK_SIZE);
3131
assert(compressedData['cachedChunks'][i].length === LZ4.CHUNK_SIZE);
3232
}
33-
pack['metadata'].files.forEach((file) => {
33+
for (var file of pack['metadata'].files) {
3434
var dir = PATH.dirname(file.filename);
3535
var name = PATH.basename(file.filename);
3636
FS.createPath('', dir, true, true);
@@ -40,29 +40,27 @@ addToLibrary({
4040
start: file.start,
4141
end: file.end,
4242
});
43-
});
43+
}
4444
// Preload files if necessary. This code is largely similar to
4545
// createPreloadedFile in library_fs.js. However, a main difference here
4646
// is that we only decompress the file if it can be preloaded.
4747
// Abstracting out the common parts seems to be more effort than it is
4848
// worth.
4949
if (preloadPlugin) {
5050
Browser.init();
51-
pack['metadata'].files.forEach((file) => {
52-
var handled = false;
51+
for (var file of pack['metadata'].files) {
5352
var fullname = file.filename;
54-
preloadPlugins.forEach((plugin) => {
55-
if (handled) return;
53+
for (var plugin of preloadPlugins) {
5654
if (plugin['canHandle'](fullname)) {
5755
var dep = getUniqueRunDependency('fp ' + fullname);
5856
addRunDependency(dep);
5957
var finish = () => removeRunDependency(dep);
6058
var byteArray = FS.readFile(fullname);
6159
plugin['handle'](byteArray, fullname, finish, finish);
62-
handled = true;
60+
break;
6361
}
64-
});
65-
});
62+
}
63+
}
6664
}
6765
},
6866
createNode(parent, name, mode, dev, contents, mtime) {

test/code_size/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"a.out.js": 246847,
2+
"a.out.js": 246838,
33
"a.out.nodebug.wasm": 597826,
4-
"total": 844673,
4+
"total": 844664,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

test/test_other.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15596,10 +15596,11 @@ def test_add_js_function_bigint(self, memory64, wasm_function):
1559615596

1559715597
@parameterized({
1559815598
'': ([],),
15599+
'lz4': (['-sLZ4'],),
1559915600
'pthread': (['-g', '-pthread', '-Wno-experimental', '-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME'],),
1560015601
})
1560115602
def test_preload_module(self, args):
15602-
if args:
15603+
if '-pthread' in args:
1560315604
self.setup_node_pthreads()
1560415605
# TODO(sbc): This test is copyied from test_browser.py. Perhaps find a better way to
1560515606
# share code between them.
@@ -15649,7 +15650,7 @@ def test_preload_module(self, args):
1564915650
}
1565015651
''')
1565115652
expected = 'done\n'
15652-
if args:
15653+
if '-pthread' in args:
1565315654
expected = "sharedModules: { '/library.so': Module [WebAssembly.Module] {} }\ndone\n"
1565415655
self.do_runf('main.c', expected, cflags=['-sMAIN_MODULE=2', '--preload-file', '[email protected]', '--use-preload-plugins'] + args)
1565515656

0 commit comments

Comments
 (0)