Optimized compact method by using a counter in place of Array.prototype.push()