title: Async/Await Cloud functions for Firebase, slow performance
body: <p>I need some help and support in solving a performance problem in my cloud functions for Firebase. I'm using async/await in combination with try/catch blocks to make my code more readable. </p>
  
  <p>I have a simple function that writes on two locations in the database, based on params key value from a queue write event. </p>
  
  <p>This simple function takes on average 1500ms to complete. Seems a little long in my opinion for a simple write to database task. I think that I made a mistake in my code and that a part of the code is not resolving correctly. </p>
  
  <p><strong>Cloud function</strong></p>
  
  <pre><code>export const watchRegisterJobs = functions.database.ref(`queues/register/{$key}`).onWrite(
    async (event: Event&lt;any&gt;) =&gt; {
      if (!event.data.exists()) {
        return;
      }
      if (event.data.exists()) {
        const formData = event.data.val();
        let key;
        event.params&amp;&amp;(key=event.params.$key);
  
        if (key) {
          console.log('New user with the following id', key)
          try {
            await addUser(formData, key)
          } catch (error) {
            console.log('can not add user to database', error)
          }
          try {
            await addOrganization(formData, key)
          } catch (error) {
            console.log('can not add organization to database', error)
          }
          try {
           await event.data.ref.remove();
          } catch (error) {
            console.log('can not remove job')
          }
          return;
        }
      }
    }
  )
  </code></pre>
  
  <p><strong>Helper functions</strong></p>
  
  <pre><code>export const addUser = async (data, key) =&gt; {
  
    const dataToSave = {
      information: ....,
      organizations: .....
    };
  
    try {
      return await admin.database().ref(`users/${key}`).set(dataToSave);
    } catch (error) {
      return error;
    }
  }
  
  export const addOrganization = async (data, key) =&gt; {
    const organization = {
      ...
    }
    try {
      return await admin.database().ref(`organizations/${key}`).set(organization);
    } catch (error) {
      return error;
    }
  }
  </code></pre>
  
  <p><strong>Update</strong>
  Based on the comments I have made some improvements to the code. Performance is still the same, but code looks better. </p>
  
  <p><strong>Cloud function</strong></p>
  
  <pre><code>export const watchRegisterJobs = functions.database.ref(`queues/register/{$key}`).onWrite(
    async (event: Event&lt;any&gt;) =&gt; {
      if (!event.data.exists()) {
        return;
      }
  
      const formData = event.data.val();
      let key;
      if(event.params) {
        key = event.params.$key;
      }
  
      if (key) {
        console.log('New user with the following id', key)
        try {
          await addUser(formData, key)
          await addOrganization(formData, key)
          await event.data.ref.remove();
        } catch (error) {
          console.log('can not write to the database', error)
        }
        return;
      }
    }
  ) 
  </code></pre>
  
  <p><strong>Helpers</strong></p>
  
  <pre><code>export const addUser = (data, key) =&gt; {
  
    const dataToSave = {
      information: ....,
      organizations: ....
    };
  
    return admin.database().ref(`users/${key}`).set(dataToSave);
  }
  
  export const addOrganization = (data, key) =&gt; {
    const organization = {
      ...
    }
  
    return admin.database().ref(`organizations/${key}`).set(organization);
  }
  </code></pre>