Donald Trump’s favourite social media saw revenue grow to $758 million, up 29 percent.

NEW YORK — Twitter is doing more with less, at least when it comes to making money from a declining user base.

The company is purging and blocking the creation of malicious accounts and spam, including those set up to try to influence U.S. and other elections. Twitter averaged 326 million monthly users in the third quarter, 2 percent fewer than the previous quarter. It was Twitter’s first quarter-to-quarter decline since it lost 1 million in the second quarter of 2017.

But video advertising is strong, accounting for more than half of total ad sales in the quarter. Twitter said it saw solid revenue across nearly all geographic locations and types of ads.

The San Francisco company’s stock is surging as investors are forgiving the huge drop in subscribers and focusing instead on stronger-than-expected profit and revenue. In the past, investors paid more attention to user growth than ad revenue, but the company’s stated focus on the “health” of its service appears to have helped. CEO Jack Dorsey opened a conference call with analysts by saying the company was purging “spammy accounts.”

“I think they did a great job explaining it, and it was smart to lead off the call by addressing the decline directly,” Wedbush analyst Michael Pachter said.

In addition, Dorsey said that Twitter’s daily usage declined among users who access the service only through the web. Pachter noted that it is “pretty clear” that bots use the web access and aren’t tweeting from smartphones.

Twitter and other social-media companies are under intense pressure to rid their services of trolls and malicious accounts ahead of the U.S. midterm elections. In February, 13 Russians, including a businessman close to Russian President Vladimir Putin, were charged by U.S. special counsel Robert Mueller in a plot to interfere in the 2016 U.S. presidential elections through social-media propaganda. Twitter and others have since faced congressional questioning and have been trying to combat fake accounts, even as those behind them have gotten more sophisticated at skirting the blocking efforts.

With its latest quarterly report, Twitter is proving that it can make more money even with fewer users. That makes sense if the decline is due to ridding its service of malicious bots that, at least for now, aren’t known for clicking on ads or buying shoes because they saw some advertised online.

Even before Twitter took an aggressive stance on fake accounts, it had been struggling with user growth. Twitter is very well known, thanks largely to President Donald Trump’s headline-making tweets nearly every day. But this has not translated to noticeable user growth for the company.

Dorsey said Twitter has a “different dynamic than a typical social network,” though he didn’t name larger rivals such as Facebook or Instagram. He said people visit Twitter because they are interested in an event or topic such as sports — and the faster Twitter can show this to them, the better.

Twitter said it had earnings of $1.02 per share in the July-September period. Excluding one-time items, earnings were 21 cents per share, exceeding the 14 cents industry analysts had expected, according to FactSet. Revenue was $758 million, up 29 percent, and also beating expectations.

Shares spiked $4.88, or nearly 18 percent, to $32.42 in midday trading Thursday.