Thursday, March 15, 2012

Garbage Collection in JavaScript, Part 1

A recent post on Scirra claimed that reusing long-lived objects was an ingredient to good JavaScript garbage collection behavior. That made me curious. This claim is generally true when the garbage collector in question is a generational one. Generational garbage collectors split the heap (memory from where all non-stack allocated things are allocated from) into several "generations". The "generational assumption" is that short-lived objects tend to be collected more frequently than long-lived objects in the heap. Upon each garbage collection, any objects which aren't collected can be promoted to the long-lived generation. The idea is that by running garbage collection less frequently on longer-lived generations, garbage collection can feel more responsive.

The problem is that generational garbage collection isn't universal. It isn't the most responsive form of garbage collection or memory management either (concurrent and incremental garbage collectors are more advanced and optimized for realtime/low-latency applications). Furthermore, although generational garbage collectors for the major browsers are in the works [Firefox, WebKit/Safari], it appears that currently only Chrome deploys one in the release browser. In fact, Chrome V8 cites generational collection as one of the main features of V8 enabling high-performance JavaScript. Around 7/20/2012, Mozilla added an Incremental Garbage Collector to Firefox 16, which reduces GC pause times by incrementalizing collection rounds, but this says nothing of and does not rely on the generation assumption. In Lisp (the first implementations of "generation" garbage collectors by Lieberman and Hewitt [PDF] and Moon [PDF]), Smalltalk (one of the first languages with generational collectors [David Ungar's paper Generation Scavenging: A Non-disruptlve High Perfornmance Storage Reclamation Algorithm, PDF]) and the higher-order typed language work (Haskell, OCaml, Standard ML, etc.), we have been using generational collectors for quite a while.

The Scirra post suggests attempting to avoid garbage collection, but that is a very tricky matter. I think the realtime performance of such an approach would be very sensitive to the design of the garbage collector and any related heuristics. Moreover, if JavaScript interpreters move beyond generational collectors at some point, programming styles exploiting the generational assumption won't have the same payoff as before.

Continue to Part 2 in this series where I introduce profiling and benchmarking features in the Chrome V8 garbage collector.

Tuesday, March 13, 2012

Preferred Stocks

Preferred stocks have risen considerably from their lows last year. In fact, PFF (US Preferreds) outperformed the S&P 500 by 130 basis points excluding dividends. Given that the lion's share of the preferreds universe is financial, this isn't too surprising. The flip side of this rise is that preferreds are getting close even not having already exceeded their call price. Of the top 5 holdings in the PFF portfolio, I could find call information for only three of them. The top holding, GM series B preferreds trades at 42.23 but has no call feature. It is, however, convertible. The 2nd top holding HCS series B (HSBC Bank) trades at 27.33 and also has no call feature. It is not convertible. The 4th largest holding is WFC series J (Wells Fargo) which is callable at 25 but currently trades at 29.63. That amounts to a potential 15.6% loss right off the bat from a call. WFC.PFJ yields 6.78%, so you are no where near compensated for the call risk. That said, I was surprised that 2 out of the top 5 holdings did not have the call feature, so PFF, though still quite risky, may be more resilient than meets the eye. In the very least, we cannot say that its entire portfolio is callable at a moment's notice.

Monday, March 12, 2012

Asset Classes

Given the past few eventful weeks, I thought it would be interesting to review the state of the various asset classes. Below is a table with the 3-months returns for some ETF proxies for various asset classes.

Security3 mos return

Notably, gold and bonds (LAG) are underperforming the other classes after tremendous outperformance last year (2011). With the good job numbers coming out in the past couple of months, the prospective for real growth is looking a little better, thus denting the appeal of gold despite the massive central bank easing through the world. Bonds, which have been looking a negative real yields for a while now, are still holding up pretty well all things considered. Of course, Operation Twist is probably still helping quite the bit.

Friday, March 2, 2012

Permanent Portfolio

Amidst the past few years of turmoil, Harry Browne's Permanent Portfolio is an interesting case. Browne is an investment manager, author, and two-time Libertarian Presidential candidates (for some reason a whole lot of investment managers are Libertarian). The Permanent Portfolio is composed of four equally weighted components: stocks, bonds (long-dated Treasuries), cash (short-term Treasuries), and gold (bullion). The Permanent Portfolio is currently available to the public in two forms: Michael J. Cuggino's Permanent Portfolio mutual fund (PRPFX) and Global X's Permanent ETF (PERM). Of course, one can certainly build one's own permanent portfolio using individual securities and ETFs. As least from PRPFX's perspective, the lost decade was hardly a lost decade. It returned a respectable 164% for the period 2002-2011. That isn't to say that there wasn't some volatility along the way. During the depths of the crisis from June 2008 to February 2009, the portfolio experienced the maximum drawdown of 19.6% (though max quarterly loss was only 6.86%). Cuggino's execution emphasized Swiss sovereigns (in Swiss francs so there is some currency exposure) alongside US Treasuries. For the growth component, he overweights energy, miners, and REITs.

Thursday, March 1, 2012

Bid-Ask Spread

One reason for preferring highly liquid versus illiquid products is the bid-ask spread. This is a cost built into a trade when the trade is a market order. This spread also happens to be the market maker's profit for providing liquidity, whether it be a traditional specialist or a high-frequency trader taking the other side of your trade. Though I usually trade using limit orders to cut down on the spread I pay, for illiquid issues, it is really difficult to get the spread down by much if I want any timely execution.

One case I stumbled on is that of specialized ETFs. With so many specialized ETFs on the market, slicing, dicing, and grafting all different kinds of securities, some of them are bound to be too exotic to gain the trading volume of a typical S&P 500 stock. Moreover, ETF share creation and redemption mechanisms usually work to minimize costs, but this is not always the case. A case in point are the ETFs issued by brokers specifically for their own customers. Whereas SPDRS, Vanguard, and iShares ETFs trade millions of shares a day, some of these broker-tied ETFs only trade tens of thousands. These ETFs can be traded by anybody via any broker, but the customers of the issuing broker receive incentives such as commission-free trading. The price, however, is greatly reduced liquidity and large bid-ask spreads. I frequently see spreads of 0.5%+ for some of these products. For a large number of trades and a good amount (> $2000), paying the spread quickly becomes more expensive than paying a hefty commission. Large bid-ask spreads certainly aren't restricted to certain low volume equity-like products. Spreads in forex are quite lucrative for forex brokers, some of whom do not charge commission at all in favor of profiting for spreads. For currency-pairs such as the New Zealand Dollar (NZD), large spreads are quite common. In contrast, EUR-USD and JPY-USD usually maintain 1-2 pip spreads. However, during major events and announcements, even these high-volume instruments may see spreads widen considerably.