title: How to measure execution time of a particular block of code in Cloud Function for Firebase?
body: <p>I wanted to measure the execution time of a particular block of code running on google cloud function( for firebase write event). Can anyone tell me how to do it.</p>
  
  <p>Is there no specific tool to measure the execution time. </p>
  
  <p><strong>I have coded 2 codes. So, I wanted to know which code will have better execution time which in turn gives better performance.</strong></p>
  
  <p>I tried to use process.hrtime() in the following code but it yields different results for same data. </p>
  
  <p>*Algo 1 Running time 299661890</p>
  
  <p>Algo 1 Running time 5684236</p>
  
  <p>Algo 1 Running time 10185061*</p>
  
  <p><em>start time [ 87, 594147806 ]
  'Algo 1 Running time 9251749'</em></p>
  
  <p><em>start time [ 22, 803098325 ]
  'Algo 1 Running time 1498176261'</em></p>
  
  <pre><code>// Import the Firebase SDK for Google Cloud Functions.
  var functions = require('firebase-functions');
  var t0
  var mymap= new Map();
      exports.processData=functions.database.ref("/test").onWrite(event=&gt;{
      const dataValue = event.data.child('data').val()
      dataValue.body = myFuction(dataValue.body)
      const promise = event.data.ref.child('data').set(dataValue)
    '
      //Finish Time
      t1=process.hrtime(t0)
  
      var RunTime=Math.round((t1[0]*1000000000) + (t1[1]));
      console.log("Algo 1 Running time "+RunTime)
      return promise;
      }
  })
  
  function myFunction(s){
      // start time 
      t0=process.hrtime()
      var newValue=0
      myProbdict.forEach(mapElements);
      function mapElements(value, key, map) {
          if(newValue&lt; 68){
          reduction+=parseInt(value)
          var regexstring ="\\b"+`${key}`+"\\b"
          var regexp = new RegExp(regexstring, "gi");
          s= s.replace(regexp,"#")
          }
      }
      return dataValue
  }
  </code></pre>