Tech stocks fell out of favor last year as an economic downturn caused a reduction in consumer spending. However, a sell-off highlighted the industry's strength as many tech stocks retained five-year growth despite steep declines in 2022. 

Investment mogul Warren Buffett has massively profited from tech as his holdings company, Berkshire Hathaway, has dedicated 46% of its portfolio to Apple. The iPhone company's stock gained more than 570% since Berkshire first invested in 2016, illustrating how lucrative the sector can be.

As a result, it's not a bad idea to exercise a similar investment strategy with your own holdings and allocate a large portion of it to the tech market. So, here are two tech stocks to buy for the long haul. 

1. Amazon

Amazon (AMZN 3.43%) shares have increased by 60% year to date. Wall Street has become increasingly bullish about a solid recovery in the company's e-commerce earnings and growing potential in artificial intelligence (AI). The growth is an impressive improvement from its stock slide of 50% last year, with the comeback proving the resilience of its business.

Macroeconomic headwinds caused significant profit losses in Amazon's retail division last year. However, the company has enjoyed a correction in 2023, reporting an operating income of $3 billion in its North American segment in the second quarter. Comparatively, the segment experienced operating losses of $627 million in the year-ago quarter.

According to data from Statista, global e-commerce sales made up about 19% of all purchases in 2022. That figure is projected to hit 23% by 2027. Meanwhile, Amazon has massive dominance in the sector, holding a leading 38% market in the U.S. alone. 

The growth potential of Amazon's e-commerce business on its own is an excellent reason to make a long-term investment in the tech giant. However, the argument for its stock is made stronger by its growing venture in AI. The company is also home to the world's largest cloud platform Amazon Web Services (AWS), giving it an edge in the market. 

AWS introduced several AI-enabled services this year and announced an expansion into chip development, which could pay off substantially as the industry develops in the coming years. 

Additionally, despite a recent rally, Amazon's stock is down about 28% from its all-time high in July 2021. As a result, there's still plenty of room for a bull run. Meanwhile, its dominance in e-commerce and cloud computing makes its stock a screaming buy to hold over many years. 

2. Alphabet

Like Amazon, Alphabet's (GOOG 9.96%) (GOOGL 10.22%) prospects in AI have rallied investors. Its stock is up more than 45% since Jan. 1. after unveiling a competitor to OpenAI's ChatGPT, called Bard, and other AI tools. The company is integrating the technology into every part of its business, from YouTube to Google Cloud, as it works to claim market share in the high-growth industry.

Moreover, Alphabet posted positive improvements in its ad revenue this year after suffering significant declines in 2022. In Q2 2023, the company's advertising revenue rose 5% year over year, driven primarily by growth in Search and YouTube. Digital ad spending worldwide hit $567 billion in 2022, projected to reach $836 billion by 2026. Alphabet's dominance in the industry makes it well positioned to profit considerably from that growth. 

Despite Alphabet's prospects and rising revenue, the company has been overlooked among investors in favor of companies like Apple and Microsoft this year. Consequently, its forward price-to-earnings ratio is lower than both of these companies, shown in the chart below. The difference makes Alphabet one of the best-valued tech stocks.

MSFT P/E Ratio (Forward) Chart.

Data by YCharts.

Alphabet shares have climbed 115% in the last five years. In the same period, its annual revenue climbed 107%, with operating income up 130%. The company is home to some of the world's most popular brands, such as Google, Android, YouTube, and Fitbit, which granted it consistent gains and a leading position in tech. Its expansion into AI and improvements in ad revenue only strengthen its outlook and make Alphabet's stock an attractive option for the long haul.