TOKYO -- When the Persian Gulf crisis cast a pall over the world economy last month, Japan provided a ray of sunshine amid the gloom. The Japanese had labored mightily to make their industrial system more energy-efficient, and Japan's robust economy appeared better able than most to withstand the new adversity.

But now, the Japanese economic horizon is full of clouds. The prospect of war in the Middle East has dealt Tokyo stocks a terrific beating, raising the specter of an economic slowdown and a significant reduction in the flow of Japanese funds to the United States and other economies.

The result is that Japan's role as an engine of U.S. and world growth will be sharply curtailed. The stock market decline has inflicted punishing losses on the investments held by Japanese banks. Experts say the banks will have no choice but to curb their huge overseas lending activities, which helped to power the world expansion over the past eight years. "It's the end of an era," said one U.S. official.

The outcome could be particularly troublesome for the debt-burdened U.S. economy, which has grown dependent on the flow of loans and investments from Japanese banks and insurance companies.

Moreover, forecasters widely expect that the Japanese economy, which has been racing along at a 5-percent-plus growth rate in 1990, will slow next year to a rate of expansion between 3 percent and 4 percent. That is still a solid pace by U.S. standards, but it is "not enough for Japan to be a locomotive for the world economy," said Kenneth Courtis, senior economist with Deutsche Bank Capital Markets (Asia).

Developments over the past several days have hardly been encouraging. Japanese interest rates have surged recently, and, following a 4.75 percent drop in share prices on Wednesday, the Nikkei stock index fell by another 2.15 percent Thursday, closing at 21,478.71 points. In morning trading today, the Nikkei dropped another 548 points, or 2.66 percent, to close at 21,223.14. The index has shed more than 2,000 points since Tuesday.

While signs of weakness abound, virtually no economists are forecasting that Japan will fall into recession; only a calamity such as a cutoff of Middle Eastern oil would cause economic growth to turn negative, analysts say. The United States, by contrast, is already in a downturn.

The Japanese have been on a consumption binge, and they show no sign of letting the gulf crisis or the stock market's woes dampen their desire for spending the money they accumulated during the 1980s. Department store sales rose a smart 7.9 percent last month, according to government figures released Wednesday.

An even more potent factor likely to keep the economy from sputtering to a halt is spending by Japanese companies on plants and equipment. Corporate outlays for major investments have surged at double-digit rates for three consecutive years. Japan's corporate managements, famous for their focus on long-term payoffs and maintaining market share, "never want to be the first to cut back," noted Patricia Kuwayama, an economist with J.P. Morgan & Co. in Tokyo.

Even after the Iraqi invasion of Kuwait, an August survey of Japanese companies showed that they revised their 1990 spending plans upward, to an increase of 12.1 percent.

But the stock market plunge, combined with higher interest rates, is virtually certain to slow capital spending in 1991 -- and, with it, the overall Japanese economy, Kuwayama and other analysts say. That is because higher rates and lower stock prices make it more costly for companies to raise the funds they need for major investments.

Of most concern, though, are the woes facing the once-vaunted banking system.

Along with banks in other industrial nations, Japan's are struggling to meet new international requirements for financial soundness that are scheduled to take effect in 1993. But the stock market plunge has wiped out billions of dollars worth of the value of their holdings, putting the financial cushion of many Japanese banks below the required levels, according to Salomon Brothers Asia Ltd.

To comply with the new legal requirements, the banks are already cutting back on loans to marginal customers, according to Japanese press reports. And the greatest impact will almost certainly be felt by the Japanese banks' foreign borrowers. "It's easier for Japanese banks to cut lending abroad than with customers here with whom they have long-standing relationships," said Robert Feldman, a Tokyo-based economist for Salomon.

Worries about a reduction in Japanese bank lending come at a time when statistics have already shown that other big Japanese financial institutions -- such as insurance companies -- have been bringing their money home from the United States, partly because investment returns have become more attractive in Japan as interest rates have risen.

To make matters worse, another major country known during the 1980s for its capital exports -- West Germany -- has also begun shifting funds home to help finance the reconstruction of East Germany.

"Japan and West Germany have been playing the role of locomotives, but I think they are both at their peak," said Deutsche Bank's Courtis.

The implications for the United States are disturbing. A drying-up of funds from Japan and Germany, Courtis said, could wreck the efforts by Federal Reserve Board Chairman Alan Greenspan to keep the U.S. economy moving forward by gingerly easing credit. "If money isn't coming in," Courtis said, "then it undoes everything Greenspan has been trying to do."