| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,40 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const fs = require('fs'); | ||
| 3 | + const path = require('path'); | ||
| 4 | + const common = require('../common.js'); | ||
| 5 | + const { strictEqual } = require('assert'); | ||
| 6 | + | ||
| 7 | + const tmpdir = require('../../test/common/tmpdir'); | ||
| 8 | + const benchmarkDirectory = | ||
| 9 | + path.resolve(tmpdir.path, 'benchmark-esm-parse'); | ||
| 10 | + | ||
| 11 | + const bench = common.createBenchmark(main, { | ||
| 12 | + n: [1e2] | ||
| 13 | + }); | ||
| 14 | + | ||
| 15 | + async function main({ n }) { | ||
| 16 | + tmpdir.refresh(); | ||
| 17 | + | ||
| 18 | + fs.mkdirSync(benchmarkDirectory); | ||
| 19 | + | ||
| 20 | + let sampleSource = 'try {\n'; | ||
| 21 | + for (let i = 0; i < 1000; i++) { | ||
| 22 | + sampleSource += 'sample.js(() => file = /test/);\n'; | ||
| 23 | + } | ||
| 24 | + sampleSource += '} catch {}\nexports.p = 5;\n'; | ||
| 25 | + | ||
| 26 | + for (let i = 0; i < n; i++) { | ||
| 27 | + const sampleFile = path.join(benchmarkDirectory, `sample${i}.js`); | ||
| 28 | + fs.writeFileSync(sampleFile, sampleSource); | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + bench.start(); | ||
| 32 | + for (let i = 0; i < n; i++) { | ||
| 33 | + const sampleFile = path.join(benchmarkDirectory, `sample${i}.js`); | ||
| 34 | + const m = await import('file:' + sampleFile); | ||
| 35 | + strictEqual(m.p, 5); | ||
| 36 | + } | ||
| 37 | + bench.end(n); | ||
| 38 | + | ||
| 39 | + tmpdir.refresh(); | ||
| 40 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -56,7 +56,24 @@ const { getOptionValue } = require('internal/options'); | |||
| 56 | 56 | const experimentalImportMetaResolve = | |
| 57 | 57 | getOptionValue('--experimental-import-meta-resolve'); | |
| 58 | 58 | const asyncESM = require('internal/process/esm_loader'); | |
| 59 | - const cjsParse = require('internal/deps/cjs-module-lexer/lexer'); | ||
| 59 | + | ||
| 60 | + let cjsParse; | ||
| 61 | + async function initCJSParse() { | ||
| 62 | + if (typeof WebAssembly !== 'undefined') { | ||
| 63 | + const { parse, init } = | ||
| 64 | + require('internal/deps/cjs-module-lexer/dist/lexer'); | ||
| 65 | + await init(); | ||
| 66 | + let exports; | ||
| 67 | + try { | ||
| 68 | + ({ exports } = parse('exports.a=1')); | ||
| 69 | + if (exports.length === 1) { | ||
| 70 | + cjsParse = parse; | ||
| 71 | + return; | ||
| 72 | + } | ||
| 73 | + } catch {} | ||
| 74 | + } | ||
| 75 | + cjsParse = require('internal/deps/cjs-module-lexer/lexer'); | ||
| 76 | + } | ||
| 60 | 77 | ||
| 61 | 78 | const translators = new SafeMap(); | |
| 62 | 79 | exports.translators = translators; | |
@@ -167,6 +184,7 @@ translators.set('commonjs', async function commonjsStrategy(url, isMain) { | |||
| 167 | 184 | if (isWindows) | |
| 168 | 185 | filename = StringPrototypeReplace(filename, winSepRegEx, '\\'); | |
| 169 | 186 | ||
| 187 | + if (!cjsParse) await initCJSParse(); | ||
| 170 | 188 | const { module, exportNames } = cjsPreparseModuleExports(filename); | |
| 171 | 189 | const namesWithDefault = exportNames.has('default') ? | |
| 172 | 190 | [...exportNames] : ['default', ...exportNames]; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -250,6 +250,7 @@ | |||
| 250 | 250 | 'deps/acorn-plugins/acorn-private-methods/index.js', | |
| 251 | 251 | 'deps/acorn-plugins/acorn-static-class-features/index.js', | |
| 252 | 252 | 'deps/cjs-module-lexer/lexer.js', | |
| 253 | + 'deps/cjs-module-lexer/dist/lexer.js', | ||
| 253 | 254 | ], | |
| 254 | 255 | 'node_mksnapshot_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)node_mksnapshot<(EXECUTABLE_SUFFIX)', | |
| 255 | 256 | 'mkcodecache_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)mkcodecache<(EXECUTABLE_SUFFIX)', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,7 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + require('../common'); | ||
| 4 | + | ||
| 5 | + const runBenchmark = require('../common/benchmark'); | ||
| 6 | + | ||
| 7 | + runBenchmark('esm', { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -54,7 +54,6 @@ const expectedModules = new Set([ | |||
| 54 | 54 | 'NativeModule internal/modules/cjs/helpers', | |
| 55 | 55 | 'NativeModule internal/modules/cjs/loader', | |
| 56 | 56 | 'NativeModule internal/modules/esm/create_dynamic_module', | |
| 57 | - 'NativeModule internal/deps/cjs-module-lexer/lexer', | ||
| 58 | 57 | 'NativeModule internal/modules/esm/get_format', | |
| 59 | 58 | 'NativeModule internal/modules/esm/get_source', | |
| 60 | 59 | 'NativeModule internal/modules/esm/loader', | |
| Back | FazBrowse Home | New Git URL |
0 commit comments