Becoming richer by investing in stocks usually doesn't happen overnight. It takes years -- sometimes decades -- of patience and holding on to shares of companies that aren't always performing as well as you'd like. That's an important part of the process. Investors shouldn't give up on stocks that aren't keeping pace with the market for a year or two, provided these stocks have solid prospects.

Let's consider two companies that have lagged the market over the trailing 12 months but are still worth buying: CVS Health (CVS -0.22%) and Fiverr (FVRR 3.74%).

1. CVS Health

Pharmacy chain giant CVS Health dealt with several issues last year, including disappointing financial results and losing some business from a health insurer, Blue Shield California. The healthcare company also revised its guidance downward a couple of times last year, something the market doesn't like.

CVS isn't out of the woods just yet. The company once again changed its guidance for 2024 in the wrong direction. It now expects adjusted earnings per share (EPS) of at least $8.30, compared to its previous projection of at least $8.50. The company also decreased its cash flow from operations guidance by half a billion dollars to about $12 billion. The company did so partly because it expects higher medical costs in 2024 than previously anticipated.

So, CVS could remain somewhat vulnerable in the short term, but there is much to like about its long-term prospects. CVS has a solid track record. It has a diversified healthcare business that should allow it to profit from long-term trends for a long time, and a strong competitive advantage. As shown by CVS' track record, the company has generally reported improving financial results.

CVS Free Cash Flow Chart

CVS Free Cash Flow data by YCharts

Its revenue and earnings have been skewed in recent years due to the effect -- positive and negative -- of COVID-19 vaccinations. That's why it's essential to look at the overall picture.

Also note that CVS isn't just a pharmacy chain. The company is a leading insurer through its subsidiary, Aetna, and has dipped its toes in the primary care market through acquisitions. CVS is seeking to go further by developing generic drugs through a newly launched and wholly owned subsidiary, Cordavis.

The company wants to take care of patients at virtually all levels of their medical care journey, from primary care to drug prescriptions and insurance, and, of course, the ability to offer cheaper generic versions of popular medicines. With an aging population, the demand for these services will only increase.

Lastly, CVS' brand name has built trust with patients over time, an important factor in all industries, especially in the healthcare sector. People have been going to the company's pharmacies for decades to fill their prescriptions. It will be challenging to erode the company's deeply entrenched position in thousands of markets across the U.S.

Here's one more reason to invest in CVS: The company is a solid dividend stock, currently yielding 3.58%, which should help further boost long-term returns for those who opt to reinvest their payouts.

2. Fiverr

Fiverr's platform helps freelancers attract individuals and businesses that want their services. Though the company was highly popular in the earlier days of the pandemic, business has cooled down. Revenue growth has slowed, as has the increase in the number of buyers' active accounts on its website. However, the company has made tremendous progress elsewhere, especially on the bottom line, thanks to aggressive cost-cutting measures.

Last year, Fiverr's revenue increased by 7.1% to $361.4 million. It reported a net income of $3.7 million, compared to the net loss of $71.5 million recorded in 2022. Fiverr's gross margins also grew to 82.9%, up from 80.5% year over year. True, active buyers decreased 5% year over year to 4.1 million, but the company's spending per buyer in the fourth quarter jumped 6% year over year to $278.

Fiverr's future is tied to the gig economy. The more people choose to start freelancing -- even part-time -- and the more businesses look for freelancing services, the more the company should benefit. Here's the good news for Fiverr: The gig economy should continue growing, according to projections. This shouldn't come as a surprise.

Businesses benefit from it as it helps them onboard freelancers quickly to perform certain jobs, without needing to hire full-blown employees who would be entitled to many benefits freelancers don't get. On the other hand, many freelancers enjoy the freedom and flexibility the lifestyle affords. Fiverr is a pioneer in its field.

The company also benefits from the network effect, with freelancers and businesses increasingly seeking one another on the platform, making it more valuable over time. Fiverr sees a total addressable market worth $247 billion. Though it isn't the only company in this industry, even grabbing 2% of that total in the next 10 years would work wonders for its top and bottom lines.

That's why the stock could provide much better returns in the next 10 years than it has over the past 12 months.