Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slow require/import on serverless functions #525

Closed
mark-veenstra opened this issue Jan 29, 2020 · 4 comments · Fixed by #526
Closed

Slow require/import on serverless functions #525

mark-veenstra opened this issue Jan 29, 2020 · 4 comments · Fixed by #526
Labels
code-generation issue deals with generated code difficulty: hard fix is hard in difficulty status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap

Comments

@mark-veenstra
Copy link

Issue Summary

Requiring the twilio-node library takes a few seconds when using it in a Docker container or in an own server setup, maybe the issue is not that important. Since it will load into memory and then it is available during the lifetime of the Node process.

But if you want to use twilio-node in a serverless environment cold starts of the function can be done very often. It seems that all functions where we require/import twilio-node takes some time.

Is there anyway to import parts of the twilio-node package instead of setting everything up? I think that may speed up the import/require. Or any other ideas?

Steps to Reproduce

  1. Create a serverless function on Firebase or AWS
  2. Add code like:
import * as twilio       from 'twilio';
const client = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
console.log('Loaded');

Exception/Log

On Firebase Cloud Function the execution takes:

Function execution took 2872 ms, finished with status code: 200

Technical details:

  • twilio-node version: 3.39.2
  • node version: 8.10.1
@nahuef
Copy link

nahuef commented Feb 3, 2020

Probably not related, but AWS dropped support for node 8.x, you should update your runtime.
More info

@mark-veenstra
Copy link
Author

On Firebase default is still node 8, but even when we use node 10 the problem exists. Partial initialization of the twilio-node would help.

@wolfenrain
Copy link
Contributor

I have done some tweaking and added a proof of concept lazy loading to the Twilio module to see if it improves the loading time, see the compare for reference.

When lazy loading is disabled(default), test returns a 600ms timeframe:

console.time('Twilio');
const Twilio = require('./index');
const client = new Twilio('AAAAAAAAAAAAAAAAAAAAAAAA', 'AUTHTOKEN');
console.timeEnd('Twilio');

When lazy loading is enabled, test returns a 16ms timeframe:

console.time('Twilio');
const Twilio = require('./index');
const client = new Twilio('AAAAAAAAAAAAAAAAAAAAAAAA', 'AUTHTOKEN', { lazyLoading: true });
console.timeEnd('Twilio');

The require of Twilio has been changed slightly for the initializer, instead of auto requiring everything, I made it into getter. This already gave a small increase. But the biggest change was removing all the requires from Twilio.js. I moved those to getters as well.

And the requiring of the RestClient.js is also a big time consumer, so I made a getter for that as well.

Problem is that the Twilio.js file is automatically generated.. but my compare should at least show that certain optimizations are possible. It would be great if the code generator is able to generate these optimizations.

@childish-sambino
Copy link
Contributor

@wolfenrain Awesome! If you wouldn't mind submitting a PR with your changes and then we can use that as a template for making the code generator changes.

@childish-sambino childish-sambino added code-generation issue deals with generated code difficulty: hard fix is hard in difficulty status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap labels Feb 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code-generation issue deals with generated code difficulty: hard fix is hard in difficulty status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants