walker_nav navigation menu causing exceptionally high TTFB

I am in the process of finishing a website for my own business, and I have tried to do my best to learn and put together my own WordPress site. This is my issue:

My website currently has an extremely high TTFB (>10s)

I seem to have isolated the problem down to the walker class being used to create the mega menu for my website.

When I disable the menu navigation from the WordPress backend, header.php & functions.php, the TTFB drops to about (<1.5 seconds), so I am now fairly sure the issue is being caused in the code executing the custom navigation menu.

I am posting the code below and I have to admit I am an absolute amateur and even though I superficially understand what the code is doing, I absolutely cannot recognize the inefficiencies in this code since I do not have the relevant experience that would lead to such insights.

Any help would be greatly appreciated!

Walker.php code:

    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ){
    
    
    $postid = url_to_postid( $item->url );
    $thumbnail = get_the_post_thumbnail_url($postid);
   
       
    
    if ( array_search( 'menu-item-has-children', $item->classes )&& $depth==0 ) {
        
        
        
     $output .= sprintf( "n<li class='topnavitem %s'><a href="http://wordpress.stackexchange.com/%s" class="topnavtext">%s</a> <span class='plus'><i class='fas fa-angle-down'></i></span>n", ( array_search( 'current-menu-item', $item->classes ) || array_search( 'current-page-parent', $item->classes ) ) ? '' : '', $item->url, $item->title );
    } 
    
    elseif ( array_search( 'menu-item-has-children', $item->classes )&& $depth==1 ) {
        $output .= sprintf( "n<li class='productsnav %s'><a href="http://wordpress.stackexchange.com/%s" class="productsnavtext">%s</a><span class ='sub-plus'><i class ='fas fa-angle-down'></i></span>n", ( array_search( 'current-menu-item', $item->classes ) || array_search( 'current-page-parent', $item->classes ) ) ? '' : '', $item->url, $item->title );
    } 
    
    
    
    elseif ($depth==2){
        $output .= sprintf( "n<li class='midnavitem %s'><div class='pictext'><img src="https://wordpress.stackexchange.com/%s"/><a href="http://wordpress.stackexchange.com/%s" class="midnavtext$postid">%s</a></div>n", ( array_search( 'current-menu-item', $item->classes) ) ? '' : '', $thumbnail,$item->url, $item->title );
    }
    
    
    
    
    else {
        $output .= sprintf( "n<li class='topnavitem %s'><a href="http://wordpress.stackexchange.com/%s" class="topnavtext">%s</a>n", ( array_search( 'current-menu-item', $item->classes) ) ? '' : '', $item->url, $item->title );
    }
}

function start_lvl( &$output, $depth ) {
    $indent = str_repeat( "t", $depth );
    if ($depth == 0) {
    $output .= "n$indent<ul class="nav-level-one" role="submenu">n";
    }


        if ($depth == 1) {
        $output .= "n$indent<ul class="nav-level-two" role="subsubmenu">n";
    }
    
}