title: Best practice for creating multiple lightweight Google Cloud Functions?
body: <p>The way it seems Google Cloud Functions works is by:</p>
  
  <ul>
  <li>your modules go inside a <code>functions</code> directory</li>
  <li>that <code>functions</code> directory then contains a <code>package.json</code> file to contain the shared dependencies across all your modules</li>
  <li>modules can contain many exported functions per module</li>
  <li>google cloud functions and firebase functions have different opinions how to handle exported functions in modules
  
  <ul>
  <li>for gcp, it seems to work like this: the module is uploaded, and then you specify via the web interface or command line which exported method should be called from the loaded module</li>
  <li>for firebase, it seems to work like this: the listener methods from <code>firebase-functions</code> return the handler, but also attaches to that handler the trigger meta-data. The <code>firebase-tools</code> cli app then requires your code locally, grabs the exported functions, then creates cloud functions for each exported method based on its attached meta-data from the firebase-functions stuff. As such, if you put all your cloud functions in the same module, then it will deploy that module multiple times for each cloud function, and for each cloud function the entire module is loaded, and then the specific exported function is called. </li>
  </ul></li>
  <li>if you configure a exported function to be a http trigger, it uses an undefined version of express.js, and an amorphous amount and order of bundled middlewares</li>
  </ul>
  
  <p>This is strange as:</p>
  
  <ul>
  <li>say even if the modules <code>one.js</code> and <code>two.js</code> require different packages at runtime, the shared <code>package.json</code> between them means that their startup time will be slower than if done individually as they will both need to install all the dependencies of the package rather than just their own</li>
  <li>if you have several exported functions inside <code>index.js</code>, such as <code>hi()</code> and <code>hello()</code>, then the <code>hi</code> cloud function will also have the <code>hello()</code> function loaded in memory despite not using it, as well as the <code>hello</code> cloud function will have <code>hi()</code> in memory despite not using it, as for both the resulting cloud function will still use the same <code>index.js</code> file, loading everything inside that module into memory even if other parts aren't needed</li>
  </ul>
  
  <p>As such, what is the best practice for making sure your cloud functions run optimally with the lightest runtime footprint possible? As it seems the design decisions by Google mean that the more cloud functions you make, then the more junk gets bundled with each cloud function, slowing them down and costing more.</p>
  
  <hr>
  
  <p>As an side, it seems to me that this would have been a better approach for google: Each cloud function should have its own directory, and in each directory there is a <code>package.json</code> file and a <code>index.js</code> file. The index.js file then does a <code>module.exports = function(...args){}</code> or a <code>export default function(...args){}</code>.</p>
  
  <p>This way the architecture aligns with how one expects cloud functions to operate - being that a cloud function represents a single function - rather than a cloud function being the installation of the shared dependencies between your all your cloud functions, then the loading of a module that can contain multiple cloud functions but only one is used, then the execution of only one function out of that loaded module.</p>
  
  <p>Funnily enough, Azure Functions seems be designed exactly the way I expect cloud functions to operate: <a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node" rel="noreferrer">https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node</a></p>