In this video we are going to see using Node.js s modules and top level weight statements in a lambda
and how it could be benefit in your application.
So actually there is an article written by Dan Fox, Principal Spatial Solutions Architect in the service
part of the ADA.
Yes.
So in this article, Dan Fox is developing an example project and compares to Node.js is modules with
the common JS modules with using top level statements.
If you open this article, he basically explains how Node.js is modules can help performance of lambda
functions.
This is especially useful when used with the provision concurrency and reduce cold start times.
You can see that it is starting designating a function as a model for that purpose.
It is adding the type module statement into package JSON and in the index case it is using the import
keyword and using the export codes in order to expose this 100 method.
This is the lambda function and we will use this format as we checked our case management console.
These are the using exports handler, but from now on we will use a script six.
So that's why we will design our lambda functions, be using the latest version of the script.
So that's why we will export the 100 method in our lambda function.
We're using the export console handler.
So if you scroll down, he's explaining very good topics, for example, explaining the provision concurrency
in lambda function and how Node.js will perform for these concurrent concurrency on lambda function.
And the code start to improving the code start benefits.
We should use the top level sync eight function because this allows the developers to use async pivot,
which is basically perform the asynchronous initialization called before the 100 invocation.
This is very important.
So that means we can use Node.js functions with async, await keywords in the script six and modularized
version three of the SDK.
It is performing the asynchronous initialization and before 100 invocation the code already initialize
for us.
So the topic is very important in this article because he is testing with two methods.
The first method is common.
JS As you can see in here, common.
JS It is using the keywords and using the exports handler to perform this operation in the lambda function
and as the same method is written with the execute modules and using the Dynamo DB Node.js JavaScript
SDK Version three package using the only client and command object and perform the same thing in the
lambda function.
And after that he is created this project that contains two lambda functions you can see in this image
and one that consisting the yes modules and another that is the common JS modules both function are
configured to respond a single API gateway endpoints and the important topic is the results.
So if you see the results here, you can find the side by side comparison of the results of two load
tests of 600 requests each and the left show of the result for the common JS module and the right shows
the result of the circuit module.
You can see for the common JS module takes 600 milliseconds while the modules takes 300 milliseconds.
A performance improvement about 50% this competition load tests.
So in order to summarize with using the secret modules and top level evade statements, we can gain
almost 50% performance advantage.
And it is very crucial when it comes to manage lambda provision concurrency and manage code start.
So as you can see that this is the best practice to use Node.js runtime in Lambda functions and using
script six modules provide serious performance benefits and we will use a async JavaScript version three
V to execute six standards when interacting with native resources into Node.js Lambda functions.
Now we have node up to use this combination into a server services and we will follow these best practices
during the course.
Now let's get started.