The S&P 500 has produced a total return of over 20% for investors in 2023, but that growth has come almost entirely from just a handful of stocks.

The "Magnificent Seven" -- Apple (AAPL -0.35%), Microsoft (MSFT 1.82%), Amazon (AMZN 3.43%), Alphabet (GOOG 9.96%) (GOOGL 10.22%), Nvidia (NVDA 6.18%), Meta Platforms (META 0.43%) and Tesla (TSLA -1.11%) -- have produced market-trouncing returns in 2023. And since they all have massive market caps, they have had an outsize impact on the cap-weighted S&P 500 index.

But just three of those seven have produced returns exceeding the S&P 500's total return (a 1% loss) since the start of last year. The other four have all lost more money for investors than a simple S&P 500 index fund.

Here are the three outperformers and the one stock I would buy for outperformance in 2024 and 2025.

NVDA Total Return Level Chart

"Magnificent Seven" total return vs S&P 500; data by YCharts.

Nvidia: 54.9% total return since Jan. 1, 2022

Nvidia is by far the best-performer of the Magnificent Seven since the start of 2022.

That's thanks almost entirely to its 2023 performance. The stock's price was cut in half in 2022, down more than 60% at one point. But shares have rocketed back in 2023, up more than 200% since the start of the year.

The big growth driver behind Nvidia has been surging demand for artificial intelligence (AI). Its graphics processing units (GPUs) are essential hardware for training advanced AI algorithms responsible for generative AI applications like OpenAI's ChatGPT or Dall-E.

Nvidia tripled its revenue in its most recent quarter, and management expects it to grow even faster in the current quarter. And with the premium pricing it's demanding for its data center AI chips, it's seeing gross margin expand as well.

That said, the market expectations are extremely high for Nvidia. A single misstep, product setback, or new competitor could send the share price tumbling. So while it could continue to outperform over the next two years, it's not getting my money.

Microsoft: 12.2% total return since Jan. 1, 2022

Microsoft's 12.2% total return since the start of 2022 is good for the second-best performance among the Magnificent Seven. But the bulk of that outperformance didn't happen until recently. In fact, Microsoft has outperformed the S&P 500 by 11.6% since Sept. 28.

AI has been the big story for the company in 2023 as well. After increasing its investment in leading generative-AI developer OpenAI at the start of the year, Microsoft Azure positioned itself as the leading cloud platform for other AI developers. That has resulted in strong growth for Azure relative to competing providers like Alphabet's Google Cloud and Amazon Web Services (AWS).

Microsoft has also developed its own generative AI solution for enterprises called Copilot, which can help sales teams, software developers, and health professionals by using AI to improve workflows. It sees just about everyone using Copilot in the workplace eventually. Considering the company's existing position as the leading enterprise software provider, it's in a great position to sell Copilot (at $30 a month per seat).

Despite recently hitting an all-time high, I believe the stock remains attractive. Still, it's not my favorite of the Magnificent Seven.

Apple: 8.2% total return since Jan. 1, 2022

Apple was the best-performing stock of the Magnificent Seven in 2022, despite underperforming the S&P 500 by more than 8 percentage points. So, despite being the weakest performer of the group in 2023 so far, it still earned its place as an overall outperformer since the start of last year.

Apple's stock has performed well despite its falling revenue this year. Its total revenue declined 2.8% for the fiscal year ended in September, and revenue fell in each quarter of the year.

Nonetheless, Apple showed strength in its earnings per share (EPS) thanks in part to its expanding profit margins from a shift toward more service revenue and its robust share-repurchase program. EPS grew 13% last quarter despite the decline in revenue.

With signs of a recovery in smartphone sales growth, Apple is poised to see a return to revenue growth and faster earnings growth. What's more, the company is investing heavily in artificial intelligence and could benefit from demand for on-device AI applications that keep users' data private and secure.

The stock is attractive, even at a relatively high price-to-earnings ratio. That's because its share repurchase program and ample cash reserves justify the high price investors will have to pay today to own its shares.

But there's one stock I like even more than Apple.

The Magnificent Seven stock most likely to outperform in 2024 and 2025

If I could only buy one of the Magnificent Seven, it would be Alphabet.

The Google parent company underperformed the S&P 500 by more than 8.5 percentage points since the start of 2022, besting only Amazon and Tesla in the group. And despite a 47% run in the stock price since the start of 2023, shares still look undervalued.

Google stands to benefit from a reacceleration in digital advertising spending. While Meta has seen its revenue growth top 20% again and its margins balloon, Google hasn't quite kept pace. I expect it to close that gap as it invests in AI tools to improve discovery on YouTube and facilitate advertising in the same way Meta did.

What's more, Google Cloud stands to be one of the main beneficiaries of continued investments in AI among enterprise customers. While it'll compete with Amazon and Microsoft for customers, it's not a winner-take-all market. All three should see benefits.

With Alphabet's shares currently trading for less than 20 times analysts' consensus 2024 earnings estimate, the stock is a bargain. That's especially true considering expectations for earnings growth of nearly 20% over the next five years. While many of the Magnificent Seven still look attractive at today's prices, Alphabet is the best of the bunch.