|
| 1 | +# Performance > Dynamic Imports |
| 2 | + |
| 3 | +A [dynamic import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) is a function-like expression that allows loading an ECMAScript module asynchronously and dynamically. Unlike the declaration-style counterpart, dynamic imports are only evaluated when needed, and permit greater syntactic flexibility. |
| 4 | + |
| 5 | +Dynamic imports were introduced in ECMAScript 2020, and they have the following benefits: |
| 6 | + |
| 7 | +- **Lazy Loading**: You can load modules only when they are needed, improving the initial loading time of your application. This can be particularly useful for large applications where loading all modules at once might be inefficient. |
| 8 | +- **Code Splitting**: Dynamic imports enable code splitting, which means you can split your code into smaller chunks and load them on demand. This can lead to a smaller initial bundle size per chunk, reducing the time it takes to load your application. |
| 9 | +- **Improved Performance**: By loading modules asynchronously, you can prevent blocking the main thread, which can improve the performance of your application, especially in cases where modules are large or take time to load. |
| 10 | +- **Reduced Memory Usage**: Since modules are loaded only when needed, you can reduce the amount of memory your application uses, especially if you have a large number of modules. |
| 11 | +- **Better Resource Management**: Dynamic imports allow for better resource management, as you can control when and how modules are loaded, making it easier to manage dependencies and avoid unnecessary loading. |
| 12 | + |
| 13 | +The dynamic imports are usually used in application code for the mentioned benefits. But they can be used in dependencies too. Since the AWS SDK for JavaScript v3 supports Node.js 14+ (ES2020), we can use Dynamic imports to only load the modules when required. This is especially helpful in reducing the Lambda cold start times. |
| 14 | + |
| 15 | +In v3, the dynamic imports are used in credential providers. You can verify the module cache on loading the credential providers in JS SDK v2 vs v3. |
| 16 | + |
| 17 | +```console |
| 18 | +$ echo 'const fs = require("fs"); |
| 19 | + |
| 20 | +const cachedFilesBefore = Object.keys(require.cache); |
| 21 | + |
| 22 | +const getCacheStats = (modulePath) => { |
| 23 | + require(modulePath); |
| 24 | + const cachedFilesAfter = Object.keys(require.cache); |
| 25 | + |
| 26 | + const cachedFiles = cachedFilesAfter.filter((file) => !cachedFilesBefore.includes(file)); |
| 27 | + |
| 28 | + const noOfFilesInCache = cachedFiles.length; |
| 29 | + let bytesInCache = 0; |
| 30 | + cachedFiles.forEach((filePath) => { |
| 31 | + bytesInCache += fs.statSync(filePath).size; |
| 32 | + }); |
| 33 | + |
| 34 | + return { |
| 35 | + noOfFilesInCache, |
| 36 | + bytesInCache, |
| 37 | + }; |
| 38 | +}; |
| 39 | + |
| 40 | +module.exports = { getCacheStats };' > getCacheStats.js |
| 41 | +``` |
| 42 | + |
| 43 | +```console |
| 44 | +$ npm install [email protected] --save-exact |
| 45 | + |
| 46 | +$ echo 'const { getCacheStats } = require("./getCacheStats"); |
| 47 | +console.log(getCacheStats( |
| 48 | + "aws-sdk/lib/credentials/credential_provider_chain" |
| 49 | +));' > cache-v2.js |
| 50 | + |
| 51 | +$ node cache-v2.js |
| 52 | +{ noOfFilesInCache: 53, bytesInCache: 397715 } |
| 53 | +``` |
| 54 | + |
| 55 | +```console |
| 56 | +$ npm install @aws-sdk/[email protected] --save-exact |
| 57 | + |
| 58 | +$ echo 'const { getCacheStats } = require("./getCacheStats"); |
| 59 | +console.log(getCacheStats( |
| 60 | + "@aws-sdk/credential-provider-node" |
| 61 | +));' > cache-v3.js |
| 62 | + |
| 63 | +$ node cache-v3.js |
| 64 | +{ noOfFilesInCache: 8, bytesInCache: 26127 } |
| 65 | +``` |
| 66 | + |
| 67 | +In the above code samples, we compare the number of files and bytes loaded in cache when importing equivalent credential providers. In v2, there are 53 files loaded in cache with total size of 398 kB. While in v3, there are only 9 files loaded in cache, which total size of 26 kB. |
| 68 | + |
| 69 | +If you’re bundling your application, the Dynamic imports are not supported by default in some old versions of bundlers. For example, if your setup is using `@babel/core@<7.8.0`, you need to explicitly add `@babel/plugin-syntax-dynamic-import` plugin to transform the code. |
0 commit comments