NEW YORK — Roger Federer served poorly. Closed poorly, too. And now he’s gone, beaten at the U.S. Open by an opponent ranked outside the top 50 for the first time in his career.

Looking slow and tired on a sweltering night in Arthur Ashe Stadium, the No. 2-seeded Federer double-faulted 10 times, failed to convert a trio of set points and lost 3-6, 7-5, 7-6 (7), 7-6 (3) in the fourth round to John Millman in a match that began Monday and concluded at nearly 1 a.m. on Tuesday.

It’s only the second time in Federer’s past 14 appearances at the U.S. Open that he’s lost before the quarterfinals. He is, after all, a five-time champion at the tournament, part of his men’s-record haul of 20 Grand Slam titles.

“I have so much respect for Roger and everything he’s done for the game. He’s been a hero of mine, and today he was definitely not at his best,” Millman said, “but, you know, I’ll take it.”

So much for that highly anticipated matchup between Federer and 13-time major champion Novak Djokovic in the quarterfinals. Instead, it’ll be the 55th-ranked Millman, an Australian who had never made it past the third round at a Slam until last week, taking on No. 6 seed Djokovic.

Millman was adamant he would not be intimidated by Federer, and perhaps was helped by having spent time practicing together a few months ago ahead of the grass-court portion of this season.

Still, this was a stunner. Not simply because Federer lost — he entered the day 28-0 at the U.S. Open, and 127-1 in all Grand Slam matches, against foes below No. 50 in the ATP rankings — but how he lost. Start with this: Federer held two set points while serving for the second at 5-4, 40-15 and did not pull through. Millman knew that was the turning point.

“I felt like a bit of a deer in headlights to begin with, to be honest with you. The feet weren’t moving. Roger had me on a string. He was manipulating me around the court,” Millman said. “But I got out of a tough second set and really found my feet and started to be a little bit more aggressive.”

Then Federer had a set point in the third at 6-5 in the tiebreaker, but again was stymied.

In the fourth set, he went up a break at 4-2, yelling “Come on!” and getting all of those rowdy spectators in their “RF” gear on their feet, prompting the chair umpire to repeatedly plead for silence. But Federer uncharacteristically got broken right back with a sloppy game, most egregiously when he slapped what should have been an easy putaway into the net.

And then there was his serve.

In the final tiebreaker, he double-faulted twice in a row.

The first obvious signs of trouble for Federer came far earlier, in the second game of the second set. He started that 15-minute struggle by missing 18 of his initial 20 first serves. While he eventually held there, he needed to save seven break points along the way. It was clear the 37-year-old Federer was not at his best.

Maybe the 75 percent humidity played a role. Millman’s big rips on groundstrokes didn’t help matters. As the unforced errors mounted — Federer would finish with 77, nearly three times as many as Millman’s 28 — Federer’s wife, Mirka, couldn’t bear to look, placing her forehead on her hands in the guest box in the stands.

Federer hung his head at a changeover, a little black fan pointed right at his face, but nothing seemed to make him feel like himself.

Hours before, Djokovic left the court for a medical timeout — the second time during the tournament he’s sought help from a doctor because of harsh weather — during what would become an otherwise straightforward 6-3, 6-4, 6-3 victory over 68th-ranked Joao Sousa of Portugal.

“I’m not 21 anymore. That was 10 years ago. I still don’t feel old. But at the same time, there is a little biological clock that is not really working in your favor,” Djokovic told the crowd afterward. “Sometimes, you just have to survive.”

He reached the quarterfinals for an 11th consecutive appearance in New York as he bids for a third U.S. Open championship and 14th Grand Slam trophy.

The other quarterfinal on the bottom half of the draw will be a rematch of the 2014 U.S. Open final: No. 7 Marin Cilic against No. 21 Kei Nishikori.

Cilic, who beat Nishikori four years ago for his only major title, was a 7-6 (6), 6-2, 6-4 winner against No. 10 David Goffin, while Nishikori advanced by defeating Philipp Kohlschreiber 6-3, 6-2, 7-5.