Depending on your supported browsers, you could either go with web workers (per http://caniuse.com/#feat=webworkers IE10 is your minimum threshold), or a 'chunked' handler. Using callbacks or promises, you can pretty easily create a standard api that will support either - though the webworkers will be faster and the UI response will be smoother.
Here's a super rough example: http://plnkr.co/edit/u72tKlLR1yKvgJBsnUK9 (note that on line 5 of script.js I've disabled the web workers, as most of our browsers would use it anyway and I was hoping to show that it would work without them - just remove the && false
to reenable).
Unfortunately I've been lazy and just copied and pasted your code twice. Using XHR's, you could keep your code in one spot, then pull it in with both the web worker or the run function. The 'runner' is a pretty common thing I've done in the past to give some sort of UI feedback. It takes a "chunk" of the overall task, runs it.. then stops and waits for a second before moving on to the next chunk. Usually that's enough to allow the screen to update.