It was painful to be a net buyer of stocks in 2022. With the Nasdaq Composite falling 33% in the year, it might have seemed like every time you bought a stock, it fell 20%, 30%, or more in the following weeks. Still, tumultuous periods like this are often the best time to be investing. For reference, if you invested $10,000 in the Nasdaq Composite at the beginning of 2009, you would have over $65,300 today -- even after the drop in 2022.

I haven't let 2022 discourage me from continuing to add to my favorite companies, and it shouldn't stop you either. Here are two of my favorite that I recently bought to hold in 2023 and beyond and why you might want to do the same.

Purchase #1: Datadog

Shares of Datadog (DDOG -2.07%) got crushed in 2022, falling nearly 59%. As Datadog is a tech stock that provides performance monitoring tools and security software, it got lumped into the broader tech stock category, and nearly all companies in this industry plunged last year.

However, Datadog might not have deserved such a brutal result because the company's financial performance remained exceptionally healthy. The company's third-quarter 2022 revenue shot 61% higher versus the year-ago period, to $437 million, and the company's free cash flow followed a similar path. In the first nine months of 2022, the company generated $364 million in free cash flow -- 45% more than the free cash flow Datadog produced in the entire year of 2021.

The reasoning behind Datadog's healthy 2022 was simply that Datadog's services are critical to its 22,200 customers. Businesses use Datadog's tools to understand consumer behavior, monitor application performance, and decrease application downtime, which firms must do constantly. In other words, these services aren't optional, so Datadog didn't see demand slump as much as other tech stocks did. That's clear when looking at the company's net retention through the first nine months of 2022, which remained above 130%.

Datadog's products are sneakily resistant to macroeconomic factors, which can't be said about most tech companies. Additionally, it's the top dog in this space, according to market researcher  Gartner (NYSE: IT). With a high-quality business trading at a noteworthy discount compared to the prior year, Datadog looks like a fat pitch, and I decided to swing.

Purchase #2: Waste Management

Waste Management (WM 0.15%) is one of the few companies in my portfolio that performed quite well in 2022: Shares slumped only 6% last year. Considering other stocks plunged 30%, 40%, and even 50%, many investors would have loved to see their investments perform this well.

The likely reason for this relatively strong stock performance is that Waste Management is a dominant leader in a stable industry. The company is the top North American trash collector and recycler, with a 24% market share -- the greatest share by any individual company.

This dominance is unlikely to waver, too. There are many reasons, but the most prominent is that it would be incredibly pricey for a rival to build the fortress that Waste Management has. A landfill one acre in size can cost up to $800,000 to build, and given that Waste Management has roughly 260 active landfills, it would cost millions of dollars for an up-and-coming rival to match their scale. That's not to mention the cost of building and operating Waste Management's 340 transfer facilities, 15,500 trash collection routes, and 550 collection sites.

Waste Management has also done a fabulous job increasing its core price in 2022. The core price is a performance metric used by management to evaluate the effectiveness of its pricing strategies. Considering it reached 6.7% in the first half of 2022 -- the company's highest core price over the past seven years -- Waste Management seems to be flexing some impressive pricing power.

As a result, the company saw robust operating leverage. While revenue only jumped 9% year over year to $5.1 billion in Q3 2022, the company's net income rose far faster -- growing 19% over the same period to $639 million.

If you tack on a quickly rising dividend and impressive share buybacks, it's hard not to like Waste Management. This leader proved in 2022 that it can weather nearly any storm, which is why I decided to buy more shares of this safe stock to hold in 2023 and beyond.