Optimized compact method

Here is a relevant JSPerf link: http://jsperf.com/compact-optimization