3 ms·
It really depends what you mean by "very fast". For most event driven UI applications JavaScript can certainly be more than fast enough, even on relatively unde
by bartread 9y ago
It really depends what you mean by "very fast". For most event driven UI applications JavaScript can certainly be more than fast enough, even on relatively underpowered devices (say, iPhone 5S).
With that being said, I recently ported a bunch of data heavy JS calculation code that had previously been running in the browser (and downloading about 100MB of data to the browser to do so - ouch) to run on a .NET back-end.
(Ignore the fact that doing this on the client will sound crazy to a professional software engineer but I would argue is totally legitimate in the context of a smart person who is not a software engineer building a prototype with tools they understand, which is what happened.)
Now, in doing the port I did improve and optimise the way the calculations were performed: basically I eliminated some redundant operations and structured the data up-front in a way that made it easier to work with. This in itself only sped things up by a factor of 2 or 3.
What really gave me the performance boost was taking advantage of the strong typing that languages such as C# offer. Just switching over to using strongly typed dictionaries via generics, and strongly typed arrays, having initially used IDictionary<string, object> (this, because I just wanted to get it working to start off with, and wasn't sure I'd always be working with the same type of data), gave me an extra 75 to 100 times performance boost.
In the end the C# version outperformed the JS version by a factor of around 200, mostly due to being able to take advantage of strong typing. And here I'm comparing the classic .NET 4.6 CLR to the latest version of Google Chrome, both running on the same machine.
I'm not JavaScript bashing - I use it all the time, and it's good for what it's good for - and V8 is a fantastic runtime, but due to the constraints of the language it simply does not approach the performance of strongly typed languages. I believe I would be able to do even better than the above with C++. Due to the parallelizable nature of the calculations we are considering a switch to CUDA to boost performance further.
If you are running performance-critical CPU-intensive code, unless you have to run that in the browser, JavaScript is generally not going to be the right choice. Based on the figures I see for this renderer, neither is WebAssembly.
If you do need to run that sort of code in the browser, and you want consistent performance across browsers, then WebAssembly might be[1].
[1] As long as you don't need to support IE11. If you do need to support it I'd suggest you find a way to avoid running large amounts of JS since, whilst its performance is definitely the best of any version of Internet Explorer, it's still not what you'd call fast and doesn't do a great job of memory management.
- asgs 9y agonice comment! how does enforcing type system help improve the performance? is there any article that sheds more light? also, did you mean static typing instead of strong?
- khedoros1 9y agoStatic typing means that typechecking can be done at compile-time, and optimizations can be applied then. Dynamic typing generally means that some runtime layer needs to do typechecking during execution. So, as a rule of thumb, it's usually assumed that a statically-typed language will be faster than a dynamically-typed one.
- jbergens 9y agoThis really sounds strange. Can you put the code up somewhere? There shouldn't be a 75 times improvement by going to .NET. A 50% improvement is more likely.
- simooooo 9y agoThat is such a picked from the air number.