How to fetching data from firebase to esp32 with no delay

I would like to control the ESP32 Led from the firebase by sending OFF to turn off and sending ON to turn on. The problem is, I have set the loop delay to 1s, however, the loop function takes about 3-5 seconds for every loop. I guess the problem is because of the (Firebase.getString) function. This function is used three call functions to link (TCP connection). I would appreciate if you Could help me to solve this issue or is there any function that didn't use the three call function from firebase to ESP32?

This is the complete code, just try to run it with and without the getString function and see the delay.

#include "WiFi.h"
#include <HTTPClient.h>
#include <FirebaseESP32.h>
#include <WebServer.h>

#include <WiFiManager.h>

const char* ssid = "Mobile net";
const char* password = "hmsx21hm";
const int led2 = 18;

#define FIREBASE_HOST ""
#define FIREBASE_AUTH "Your ID"

FirebaseData DeviceStatus2;
FirebaseJson json;

void setup() {
WiFi.begin(ssid, password);
// initialize digital pin LED_BUILTIN as an output.
pinMode(led2, OUTPUT);

while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to WiFi..");

void loop() {
digitalWrite(led2, HIGH);
Serial.print("LED is ON... Normal ");

Firebase.getString(DeviceStatus2, "LED_STATUS2/Led");

if (DeviceStatus2.stringData() == "OFF") {
digitalWrite(led2, LOW);
Serial.println("Led Turned OFF");
else if (DeviceStatus2.stringData() == "ON") {
digitalWrite(led2, HIGH);
Serial.println("Led Turned ON");;


Please guys, I would appreciate if you give me any suggestion!!!

Please post the entire code.
Use autoformat in the IDE before copying, and code tags, </>, before paisting.
A wiring showing, among others, the powering often shows important facts.

Where is the Firebase instance? Whose is it?

I have published the complete code

I have published the complete code on here

Thank You.
For the future, investigate and use "Autoformat" in the IDE before copying and code tags, </>, when You paste.

I did , thank you very much for let me know.

The autoformat is a very good tool and an easy way to verify that the nesting is the intended. Mismatched, or wrongly placed, curly brackets show up directly.

Has the OP considered using the OS, freeRTOS, that is included with the ESP32? freeRTOS is a multi threading multiprocessing OS that will bring you closer to the desired outcome.