title: Firebase Cloud Function is performing super slow compared to local response time
body: <p>I’ve just deployed a cloud function to my Firebase project (free tier).</p>
  
  <p>Here's a minimal version of the code:</p>
  
  <pre><code>const admin = require('firebase-admin');
  const functions = require('firebase-functions');
  const Fuse = require('fuse.js');
  const _ = require('lodash');
  
  admin.initializeApp(functions.config().firebase);
  
  exports.minimal = functions.https.onRequest((request, response) =&gt; {
  
    let category = request.query.cat ? request.query.cat.toLowerCase().trim() : undefined;
    let classification = request.query.class ? request.query.class.toLowerCase().trim() : undefined;
    let catName, className;
  
    return admin.database().ref('categories').once('value').then(snapshot =&gt; {
  
      _.each(snapshot.val(), (value, key) =&gt; {
  
        _.each(value, (value2, key2) =&gt; {
  
          if ( key2 === 'id' &amp;&amp; value2 == category ) catName = key;
          else if ( value2.id == classification ) className = key2;
  
        });
  
      });
  
      return _.split(request.query.q, ',');
  
    })
    .then(literals =&gt; {
  
      return admin.database().ref(`mappings/${catName}/${className}`).once('value').then(snapshot =&gt; {
  
        let mappings = _.map(snapshot.val(), (value, key) =&gt; {
  
          return {
            literal: key,
            code: value
          };
  
        });
  
        const options = {
          shouldSort: true,
          includeScore: true,
          threshold: 0.3,
          location: 0,
          distance: 50,
          maximumPatternLength: 64,
          minMatchCharacterLength: 1,
          keys: ["literal"]
        };
  
        let fuse = new Fuse(mappings, options);
        let results = {};
        let classCodes = {};
  
        _.each(literals, literal =&gt; {
  
          let searchResults = fuse.search(literal);
  
          if ( ! searchResults.length ) {
  
            results[literal] = { status: 'not-matched' };
  
          }
          else {
  
            if ( ! classCodes[searchResults[0].item.code] ) {
  
              classCodes[searchResults[0].item.code] = { code: searchResults[0].item.code, standard: '' };
  
            }
  
            results[literal] = {
  
              status: 'matched',
              mapping: classCodes[searchResults[0].item.code]
  
            };
  
          }
  
        });
  
        return admin.database().ref(`classification-systems/${catName}/${className}`).once('value').then(snapshot =&gt; {
  
          _.each(snapshot.val(), (value, key) =&gt; {
  
            if ( classCodes[key] ) classCodes[key].standard = value;
  
          });
  
          response.json(results);
  
        });
  
      });
  
    })
    .catch(error =&gt; { // Never gets triggered
  
      if ( ! response._headerSent ) response.json(error)
      else console.error(error);
  
    });
  
  });
  </code></pre>
  
  <p>I was running it locally (but all database operations were performing on the live database) and the average response time for 100 words searched in 2000+ keys in the database was 1.5 seconds.</p>
  
  <p>Running the same test code on the deployed function resulted in the average response time of 45 seconds!!!</p>
  
  <p>Why is the live cloud function performing so poorly?</p>
  
  <p>Is there anyway to achieve the same response time as the local function? The current response time is insanely unacceptable.</p>
  
  <p>Any help is appreciated.</p>