Last active
August 22, 2024 11:07
-
-
Save osher/d14ad060a6da1e08608aa463f9bbd7f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this gist is an appendix for the discussion in this article: | |
# https://medium.com/@osherel/yet-another-look-on-js-require-and-import-e2a6316a161d | |
#Some setup: | |
mkdir -p sandbox/packages/a sandbox/packages/c; | |
cd sandbox; | |
echo '{"name":"sandbox","version":"1.0.0","workspaces":["packages/*"]}' > package.json; | |
npm init -y -w -q packages/a; | |
npm init -y -w -q packages/c; | |
echo 'export default "a"' > packages/a/index.js; | |
echo "exports.foo = 'bar'" > packages/c/index.js; | |
sed -i s/index.js/index.mjs/ packages/a/package.json; | |
#---------------------------------------------------------------------------------------------- | |
# requiring a single trivial module | |
cat packages/c/index.js | |
# yields: exports.foo = 'bar' | |
#in node: | |
start = process.hrtime.bigint(); require('c'); Number(process.hrtime.bigint() - start) / 1e6 | |
#---------------------------------------------------------------------------------------------- | |
# lets do that in big numbers: | |
{ | |
I=0; while [ $I -ne 500 ]; do | |
mkdir -p packages/req$I; | |
npm init -y -q -w packages/req${I} > /dev/null 2>&1; | |
echo "require('req$(($I + 1))')" > packages/req${I}/index.js; | |
[ 0 = $(((I += 1) % 10)) ] && echo -n "$I "; | |
done | |
echo "require('node:fs')" > packages/req499/index.js; | |
} | |
node -p "start = process.hrtime.bigint(); require('req0'); Number(process.hrtime.bigint() - start) / 1e6" | |
node -p "start = process.hrtime.bigint(); require('req0'); Number(process.hrtime.bigint() - start) / 1e6" | |
node -p "start = process.hrtime.bigint(); require('req0'); Number(process.hrtime.bigint() - start) / 1e6" | |
echo "require('node:fs')" > packages/req249/index.js; | |
node -p "start = process.hrtime.bigint(); require('req0'); Number(process.hrtime.bigint() - start) / 1e6" | |
node -p "start = process.hrtime.bigint(); require('req0'); Number(process.hrtime.bigint() - start) / 1e6" | |
node -p "start = process.hrtime.bigint(); require('req0'); Number(process.hrtime.bigint() - start) / 1e6" | |
echo "require('node:fs')" > packages/req99/index.js; | |
node -p "start = process.hrtime.bigint(); require('req0'); Number(process.hrtime.bigint() - start) / 1e6" | |
node -p "start = process.hrtime.bigint(); require('req0'); Number(process.hrtime.bigint() - start) / 1e6" | |
node -p "start = process.hrtime.bigint(); require('req0'); Number(process.hrtime.bigint() - start) / 1e6" | |
#---------------------------------------------------------------------------------------------- | |
# importing a single trivial module | |
cat packages/a/index.mjs | |
# yields: export default "a" | |
#in node shell: | |
start = process.hrtime.bigint(); await import('a'); Number(process.hrtime.bigint() - start) / 1e6 | |
#---------------------------------------------------------------------------------------------- | |
# lets do that in big numbers | |
# this is a 500 deep linerar dependency tree | |
rm -rf packages/imp* node_modules/imp* packages/req* node_modules/req* | |
{ | |
I=0; while [ $I -ne 500 ]; do | |
mkdir -p packages/imp${I}; | |
npm init -y -q -w packages/imp${I} > /dev/null 2>&1; | |
sed -i s/index.js/index.mjs/ packages/imp${I}/package.json; | |
echo "import 'imp$(($I + 1))'" > packages/imp${I}/index.mjs; | |
[ 0 = $(((I += 1) % 10)) ] && echo -n "$I "; | |
done | |
echo "import 'node:fs'" > packages/imp499/index.mjs; | |
} | |
#in node shell: | |
start = process.hrtime.bigint(); await import('imp0'); Number(process.hrtime.bigint() - start) / 1e6 | |
echo "import 'node:fs'" > packages/imp249/index.mjs; | |
node -p "start = process.hrtime.bigint(); import('imp0').then(() => console.log(Number(process.hrtime.bigint() - start) / 1e6))" | |
echo "import 'node:fs'" > packages/imp99/index.mjs; | |
node -p "start = process.hrtime.bigint(); import('imp0').then(() => console.log(Number(process.hrtime.bigint() - start) / 1e6))" | |
#---------------------------------------------------------------------------------------------- | |
# convert the experiment to a flat 1-generation graph with 499 dependencies | |
{ | |
rm packages/imp0/index.mjs | |
I=1; while [ $I -ne 499 ]; do | |
mkdir -p packages/imp${I}; | |
echo "export default 'imp${I}'" > packages/imp${I}/index.mjs; | |
[ 0 = "$I" ] || echo "import 'imp$(($I + 1))'" >> packages/imp0/index.mjs; | |
[ 0 = $(((I += 1) % 10)) ] && echo -n "$I "; | |
done | |
echo "import 'node:fs'" > packages/imp499/index.mjs; | |
} | |
node -p "start = process.hrtime.bigint(); import('imp0').then(() => console.log(Number(process.hrtime.bigint() - start) / 1e6))" | |
#---------------------------------------------------------------------------------------------- | |
# circular dependency | |
{#circular | |
mkdir -p packages/l1 packages/l2 packages/l3; | |
for I in 1 2 3; do | |
npm init -w packages/l$I -y > /dev/null 2<&1; | |
sed -i s/index.js/index.mjs/ packages/l$I/package.json; | |
N=$(( $I % 3 + 1 )) | |
echo "import l$N from 'l$N'; export default l$N;" > packages/l$I/index.mjs; | |
echo -n "$I "; | |
done; | |
} | |
node -e "import('l1').then(() => console.log('done'))" | |
# this fails | |
# and making it work | |
{#circular-test | |
mkdir -p packages/l1 packages/l2 packages/l3; | |
for I in 1 2 3; do | |
npm init -w packages/l$I -y > /dev/null 2<&1; | |
sed -i s/index.js/index.mjs/ packages/l$I/package.json; | |
N=$(( $I % 3 + 1 )) | |
echo "import * as l$N from 'l$N'; console.log('init $I'); export default l$N;" > packages/l$I/index.mjs; | |
echo -n "$I "; | |
done; | |
} | |
node -e "import('l1').then(() => console.log('done'))" | |
# this works | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment