Slow serial input response

Hello,
I am facing a performance problem with my Arduino Mega 2560. My loop executes for 1 second roughly. This means that my response to the request sent by the PC would be as slow as 1 second or more. Note that I am not using the Delay() function. Could be interrupt used in this case? This is a part of my code:

void communication ()
{
    String serialInput = Serial.readString();
    serialInput.trim();
  
    if(serialInput == "Data")
    {
        String unit = "";
        if(thermometerC1)
        {
          unit = "C";
          if(sensVal11)
          {
            Serial.println(unit + "#" + temp + "#" + moistureLevel1 + "#" + !errorSignal5);
          }
          else
          {
            Serial.println(unit + "#" + temp + "#Off#" + !errorSignal5);
          }
        }
        else
        {
          unit = "F";
          if(sensVal11)
          {
            Serial.println(unit + "#" + temp2 + "#" + moistureLevel1 + "#" + !errorSignal5);
          }
          else
          {
            Serial.println(unit + "#" + temp2 + "#Off#" + !errorSignal5);
          }
        }
     }
     else if(charIndex(serialInput, '#') != -1) // Maybe here is the problem?===================
     {
        String inputArr[6];
        int valuesCount = 0;
        for(int i = 0; i < serialInput.length(); i++)
        {
          if(serialInput[i] != '#')
          {
            inputArr[valuesCount] += serialInput[i];
          }
          else
          {
            valuesCount++;
          }
        }
       
       ch11menuitem = inputArr[0].toInt();
       ch12menuitem = inputArr[1].toInt();
       timesValue = inputArr[2].toInt();
       
       if(inputArr[3] == "C")
       {
        thermometerC1 = true;
       }
       else
       {
        thermometerC1 = false;   
       }      

       wateringTimeValue = inputArr[4].toInt();
       timeoutBWatValue = inputArr[5].toInt();

       settingsSave();
     }
     else if(serialInput == "ResetDefaults")
     {
       resetDefaults();
     }
  }

  int charIndex (String input, char charLookup)
    {
      for(int i = 0; i < input.length(); i++)
      {
        if(input[i] == charLookup)
        {
          return i;
        }
      }
      return -1;
    }

    void serialEvent() 
    {
      if (Serial.available()) {
        communication();
      }
    }

Every assistance would be appreciated,
Yordan Yordanov

readString blocks for one second to detetmine if you finished 'typing' :wink:

You can get ideas from serial input basics.

It also frees you from the String class that can cause memory fragmentation.

Use strcmp to compare strings when using c-strings as in the provided link.

Thank you @sterretje, the serial basics solved my problem. I appreciate your help.

Yordan