I noticed a question on here the other day ( Reducing Javascript CPU Usage ) and I was intrigued.
Essentially the guy wanted to encrypt some files character by char
Your issue is a matter of overhead vs unit of work. Your setTimeout overhead is very high while your unit of work ar.push is very low. The solution is an old optimization technique known as Block Processing. Rather than processing one UoW per call you need to process a block of UoW's. How large the "block" is depends on how much time each UoW takes and the maximum amount of time you can spend in each setTimeout/call/iteration (before the UI becomes unresponsive).
function test(i, ar, callback, start){
if ( ar === undefined ){
var ar = [],
start = new Date;
};
if ( ar.length < i ){
// **** process a block **** //
for(var x=0; x<50 && ar.length
You have to process the largest block you can without causing UI/performance issues for the user. The preceding runs ~50x faster (the size of the block).
It's the same reason we use a buffer for reading a file rather than reading it one byte at a time.