Foreign direct investment in China largely held steady during the first half of this year, even as investment inflows into the U.S. and European Union plummeted, in a fresh sign that the world’s second-largest economy has suffered less damage from the pandemic.

Globally, the monthly average for new investments for the first half of the year was down almost half on the monthly average for the whole of 2019, the largest decline on record, the United Nations’s Conference on Trade and Development said Tuesday. But while foreign investment in the U.S. and European Union fell by 61% and 29% respectively, inflows to China were down by just 4%. China attracted foreign investment totaling $76 billion during the period, while the U.S. attracted $51 billion.

The U.S. has long been the top global destination for businesses investing overseas, while China has long ranked second.

Unctad said the modest nature of the decline in foreign investment to China was surprising. Back in March, when China was the epicenter of the pandemic with significant parts of its economy in lockdown, Unctad forecast that it would be the big loser, and expected global flows of investment to fall by 15% across 2020.

However, China’s economy reopened in April just as the U.S. and Europe were in lockdown, and the country has since contained the virus with only localized and short-lived restrictions. By contrast, the U.S. and Europe have seen resurgences in infections that have slowed their recoveries. In the three months through September, China’s economy had already exceeded the levels of output recorded in the last quarter of 2019, according to data out last week.

The resilience of foreign investment in China appears to confound earlier expectations that businesses would seek to reduce their reliance on the country as a key part of their supply chains. But James Zhan, Unctad’s director of investment and enterprise, said it was too early to reach that conclusion.

“One of the main reasons for reconfiguration of global supply chains is to increase resilience, which requires backup plans and redundant capacities,” he said. “A more practical approach companies can take would be building additional production bases outside of China, which means new investment to other countries instead of divestment from China or moving production out of China.”

Across all developed economies, inflows of foreign investment were down 75% in the first half from the 2019 monthly average to total just $98 billion, a level last seen in 1994. In some cases—such as the Netherlands and the U.K.—that decline took the form of a reduction in loans from the parent company to its overseas subsidiaries, which are counted as foreign investment.

Related Video

With tensions running high, Washington and Beijing have pushed to decouple technology and trade. But American financial firms including JPMorgan and Goldman Sachs are doubling down on investing in China and expanding headcount. Photo Composite: Crystal Tai[object Object]

“In times of crisis, some multinational enterprises would like to keep funds close to home,” said Mr. Zhan. “Fear that Covid-19 and the quest for funds could lead to tax increase may also accelerate the intra-firm capital movements.”

Foreign investment in developing economies proved more resilient, falling by just 16% to $296 billion.

Unctad said there were signs of a pickup in investment during the three months through September, and it repeated its forecast that flows for 2020 as a whole would likely be 40% down on 2019. But it warned that the second wave of rising infections hitting a number of developed economies could see flows down 50% for the year.

While foreign investment in most countries fell during the first six months, a small number saw an increase. One was Germany, which saw inflows rise 15% to $21 billion, largely due to a small number of foreign acquisitions of existing businesses.

Write to Paul Hannon at paul.hannon@wsj.com