Sitelet https://web.archive.org/web/20211210154559/https://github.com/nodejs/node/commit/cb2f6ffd3e
Skip to content
Permalink
Browse files
module: use Wasm CJS lexer when available
PR-URL: #35583
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
  • Loading branch information
guybedford authored and MylesBorins committed Oct 14, 2020
1 parent 4d1d3f4 commit cb2f6ffd3e0a5f7b6766f03b97b6464720921ba9
@@ -0,0 +1,40 @@
'use strict';
const fs = require('fs');
const path = require('path');
const common = require('../common.js');
const { strictEqual } = require('assert');

const tmpdir = require('../../test/common/tmpdir');
const benchmarkDirectory =
path.resolve(tmpdir.path, 'benchmark-esm-parse');

const bench = common.createBenchmark(main, {
n: [1e2]
});

async function main({ n }) {
tmpdir.refresh();

fs.mkdirSync(benchmarkDirectory);

let sampleSource = 'try {\n';
for (let i = 0; i < 1000; i++) {
sampleSource += 'sample.js(() => file = /test/);\n';
}
sampleSource += '} catch {}\nexports.p = 5;\n';

for (let i = 0; i < n; i++) {
const sampleFile = path.join(benchmarkDirectory, `sample${i}.js`);
fs.writeFileSync(sampleFile, sampleSource);
}

bench.start();
for (let i = 0; i < n; i++) {
const sampleFile = path.join(benchmarkDirectory, `sample${i}.js`);
const m = await import('file:' + sampleFile);
strictEqual(m.p, 5);
}
bench.end(n);

tmpdir.refresh();
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

@@ -56,9 +56,26 @@ const { getOptionValue } = require('internal/options');
const experimentalImportMetaResolve =
getOptionValue('--experimental-import-meta-resolve');
const asyncESM = require('internal/process/esm_loader');
const cjsParse = require('internal/deps/cjs-module-lexer/lexer');
const { emitWarningSync } = require('internal/process/warning');

let cjsParse;
async function initCJSParse() {
if (typeof WebAssembly !== 'undefined') {
const { parse, init } =
require('internal/deps/cjs-module-lexer/dist/lexer');
await init();
let exports;
try {
({ exports } = parse('exports.a=1'));
if (exports.length === 1) {
cjsParse = parse;
return;
}
} catch {}
}
cjsParse = require('internal/deps/cjs-module-lexer/lexer');
}

const translators = new SafeMap();
exports.translators = translators;
exports.enrichCJSError = enrichCJSError;
@@ -164,6 +181,7 @@ translators.set('commonjs', async function commonjsStrategy(url, isMain) {
if (isWindows)
filename = StringPrototypeReplace(filename, winSepRegEx, '\\');

if (!cjsParse) await initCJSParse();
const { module, exportNames } = cjsPreparseModuleExports(filename);
const namesWithDefault = exportNames.has('default') ?
[...exportNames] : ['default', ...exportNames];
@@ -253,6 +253,7 @@
'deps/acorn-plugins/acorn-private-methods/index.js',
'deps/acorn-plugins/acorn-static-class-features/index.js',
'deps/cjs-module-lexer/lexer.js',
'deps/cjs-module-lexer/dist/lexer.js',
],
'node_mksnapshot_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)node_mksnapshot<(EXECUTABLE_SUFFIX)',
'mkcodecache_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)mkcodecache<(EXECUTABLE_SUFFIX)',
@@ -0,0 +1,7 @@
'use strict';

require('../common');

const runBenchmark = require('../common/benchmark');

runBenchmark('esm', { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
@@ -53,7 +53,6 @@ const expectedModules = new Set([
'NativeModule internal/modules/cjs/helpers',
'NativeModule internal/modules/cjs/loader',
'NativeModule internal/modules/esm/create_dynamic_module',
'NativeModule internal/deps/cjs-module-lexer/lexer',
'NativeModule internal/modules/esm/get_format',
'NativeModule internal/modules/esm/get_source',
'NativeModule internal/modules/esm/loader',

0 comments on commit cb2f6ff

Please sign in to comment.