views:

175

answers:

4

I've ported an Excel retirement calculator into javascript. There are 35 worksheets in the original excel containing many recursive calculations, all of which I've converted to JS. JS is running slower (1-2 seconds compared to Excel's instantaneous). I am caching the resursive calculations already to speed things up and prevent stack overflows in the browser.

Is it realistic to try and make the the JS faster? How does Excel manage to be so efficient?

I read somewhere that Excel only re-calculates when a cell's precedents have been modified. Even still, it seems to me that Excel is pretty much instantaneous no matter how much data needs to be re-calculated.

Thanks

+6  A: 

Excel is faster because it's a few layers closer to the CPU -- running compiled bytecode within the OS, rather than within a browser running interpreted JavaScript.

I'd compare performance with Google Chrome or FF3 that have a new generation of JavaScript engines and see how things improve. See John Resig's post: http://ejohn.org/blog/javascript-performance-rundown/.

Diodeus
+2  A: 

JavaScript is slower than any compiled language out there, that's why Excel is so much faster. I would use Firebug's profiler to figure out where your code is spending most of its time and focus on improving that.

Ryan Doherty
Thanks - I'm definitely going to test in Firebug. Thanks!
A: 

If you've ported the Excel formulas to JavaScript while preserving the algorithms intact, the JavaScript code that you've ended up with may not be the most ideal for JavaScript. Have you considered refactoring things to take advantage of JavaScript's powerful language features?

Also, are you rendering things (updating table cells etc.) while doing the calculations? Keep in mind that some DHTML updates may put a big burden on the browser (hey, you're running this inside a browser, right?) Perhaps separating the calculation and rendering may help. You'd first busily do all the calculations and then do the presentation as the final step.

Ates Goral
Yes, this is running inside a browser. The main development browser was IE7, but it works now in FFox, Chrome, and IE6.Writing outputs to the browser happens once all calcs are done. We use .innerHTML, which I hear is slow, too.
.innerHTML is actually the fastest way to update the DOM.
Ryan Doherty
A: 

Like other people have said, JavaScript is nowhere near as fast as a compiled language. Currently, there's somewhat of an arms race between Chrome, Firefox and Webkit's JavaScript interpreters, which has really improved the speed situation with JavaScript. However, it's still pretty slow, and if you're using IE7 (or even worse IE6), performance can be pretty dismal.

You may want to look at some of the JavaScript libraries that are out there (personally, I prefer jQuery) to see if some of them have utility functions that you could take advantage of. Some of the more heavily used JavaScript libraries may have optimized some of the work that you're trying to do. It certainly won't make JavaScript as fast as Excel, but if you can replace a lot of your functionality with utilities that have been optimized by many different people, you could see a little bit of a speed increase.

Matt Ephraim