(CNN) -- Roger Federer overcame a scare against Swiss friend Stanislas Wawrinka before finally securing a victory which ensures he will spend a record-breaking 300th week as world number one.
The 31-year-old recaptured top spot after a 17th grand slam success at Wimbledon in July and by reaching the last eight of the Shanghai Masters, he has reached another landmark in his illustrious career.
Should he win in China, Federer will claim a record 22nd Masters 1000 crown and equal John McEnroe's haul of 77 tour-level titles.
But the Swiss was pushed all the way by his David Cup teammate as the world no. 17 took the opening set 6-4 -- only the fourth set Wawrinka has won against Federer in 13 meetings.
In a tight second, Wawrinka saved a set point at 5-6 before pushing it into a tie-break. The score was locked at 3-3 but Federer found another gear to take him level.
He dropped just five points in the third set, taking it 6-0 to ensure this coming Monday will be the 300th time he has perched atop the world rankings.
"I consider myself a little lucky. I never stopped believing, pushed till the end, then got off to a good start in the third set. Basically that was it," Federer said in quotes carried by AFP.
"It was a tough match. He was the better player for basically two sets. It's great finding a way out that way, that's for sure."
"So here I am at 300 weeks. It's pretty incredible. Probably one of my biggest accomplishments. I'm very proud of that record, no doubt about it. It's a great reward for me."
He will now play Marin Cilic in the last eight after the Croatian beat Spain's Fernando Verdasco 4-6 6-1 6-4.
Federer's charge to wrestle the Shanghai crown from Andy Murray looks set to be a lot tougher, as the U.S. open champion dismissed the challenge of Ukraine's Alexandr Dolgopolov 6-2 6-2.
Murray, who also won the event in 2010, told reporters: "I think I did a decent job.
"It's very, very different conditions to last week. I think you can see by some of the results here, the guys that have come from Beijing have adapted to the conditions much, much quicker."
Murray will play Radek Stepanek after the Czech beat American John Isner 6-4 6-7 6-3 in a showdown of the big servers.
World No. 2 Novak Djokovic is gunning for Federer's mantle as the top ranked player and he says improvements to his serve have helped his game.
The Serbian defeated Spain's Fernando Lopez 6-3 6-3 and will now face Germany's Tommy Haas in the quarters after he beat Djokovic's compatriot Janko Tipsarevic 6-2 6-1.
"I'm trying to enjoy the efficiency of my serve," Djokovic told the ATP tour website. "I'm not very well known around the tour for big serving.
"But so far in this tournament, and also in Beijing, it has been working very well for me. It has been giving me a lot of free points, a lot of confidence into the matches."
France's Jo-Wilfried Tsonga will meet Tomas Berdych, from the Czech Republic, in the last eight after he beat Marcos Baghdatis 6-2 7-6. Berdych defeated American Sam Querrey 6-2 6-7 6-4.