When J.J. Barea checked into the Mavericks’ game midway through the second quarter Wednesday, he passed Devin Harris for sole possession of seventh place on the Mavericks’ all-time games played list (608 appearances).

The bit of history helped make up for Luka Doncic’s lackluster first half against the Magic and position the second-year star for second-half dominance.

Barea tallied 11 points and three assists in the last seven minutes of the period to zap the Mavericks out of their slow start in what became a 107-106 win.

Barea’s burst in his season debut, his official return from a torn Achilles tendon last January, didn’t fully erase the team’s deficit, but it helped the Mavericks keep pace as they used Doncic’s strong second half to cap its third victory in four games.

“I liked our second half a lot,” coach Rick Carlisle said. “The first half left a lot to be desired. We’ve been inconsistent at home. That’s a concern, but it’s a great win.

“For us to get where we want to go, we have to protect home court vigilantly, and we’re going to have to work at doing better.”

Doncic, the reigning Rookie of the Year and one of the NBA’s most dominant players to start this season, entered Tuesday’s contest with consecutive triple-doubles of at least 25 points, 10 rebounds and 15 assists. Only Oscar Robertson has done that trick in three straight games, during the 1961 season, but Doncic’s slow start — six points and four turnovers in the first half — left him unable to match the feat.

Enter Barea for a spark.

The 14-year veteran made his first appearance this season with about seven minutes remaining in the first half and immediately found a rhythm.

After his third 3-pointer, which helped cut the Magic’s lead to two points, Barea, who’d completed a full workout by himself at the practice facility Tuesday morning, turned away from the basket, his face red and his complexion deepening in shade with every passing second.

He clenched his fists and let out a fierce scream.

He wasn’t tired or overwhelmed in his season debut. He wasn’t angry, either. Quite the opposite.

“Barea definitely gave us a big spark, got the crowd into it, got our team into it,” Carlisle said. “Pretty amazing for a guy that has his credentials and has been sitting out for six straight games, not playing, not complaining.”

“That’s when basketball is fun,” Barea added.

In the second half, Doncic returned to the form that helped him play toe-to-toe with LeBron James last Friday, afterward earning a not-so-safe-for-work compliment from his longtime “idol.”

Doncic finished with 27 points (21 in the second half), seven rebounds, seven assists and six turnovers in a team-high 37 minutes.

He helped key a 13-3 run midway through the third quarter that gave the Mavericks their first lead.

While Magic forwards Jonathan Isaac (four steals, six blocks) and Aaron Gordon (team-high 23 points) tormented the Mavericks, Doncic was instrumental in drawing one of Gordon’s two offensive fouls in the final 35 seconds to help preserve the comeback.

“We needed that win,” Doncic said, “for sure.”