Stocks shook off a midday slide and ended with modest gains Tuesday, leaving the major indexes close to their recent all-time highs.

Small-company stocks continued to outpace the rest of the market by a wide margin, a sign that investors are becoming more optimistic about an economic rebound.

Treasury yields kept marching higher as investors anticipate that the economy will pull out of its slump and rebound later this year.

On Tuesday:

The S&P 500 rose 1.58 points, or less than 0.1%, to 3,801.19.

The Dow Jones Industrial Average gained 60 points, or 0.2%, to 31,068.69.

The Nasdaq composite added 36 points, or 0.3%, to 13,072.43.

The Russell 2000 index of smaller companies gained 36.95 points, or 1.8%, to 2,127.96, a record high.

For the week:

The S&P 500 is down 23.49 points, or 0.6%.

The Dow is down 29.28 points, or 0.1%.

The Nasdaq is down 129.54 points, or 1%.

The Russell 2000 is up 36.30 points, or 1.7%.

For the year:

The S&P 500 is up 45.12 points, or 1.2%.

The Dow is up 462.21 points, or 1.5%.

The Nasdaq is up 184.15 points, or 1.4%.

The Russell 2000 is up 153.10 points, or 7.8%.