Save Expand icon

Ron Valstar
front-end developer

JavaScript performance testing

‘All roads lead to Rome’ is a saying in Dutch that means there are multiple ways to accomplish the same thing. Very true in real life but maybe even more so in programming. You’d think the fastest code is the best, but a lot of times readability is chosen over speed (or, as in most cases, speed isn’t just that important).


A JavaScript speed test will normally look something like this:
calculate elapsed time while doing the same calculation a fixed number of times
Or like this:
count how many times we can do the same calculation in a fixed amount of time

Simple enough. But for a solid test result you have to test in multiple browsers, operating systems and machines. Luckily for us, in this day and age of cloud computing, there is something out there that does just that. But more importantly, whatever you were planning to test, chances are that somebody else already beat you to it.
Enter jsperf, a great online benchmarking application build with benchmarkjs. You can create your own tests, or run existing ones. You can even revise existing tests.

For instance: I know a lot of developers who don’t like switches because it makes their code unreadable . Some don’t even us ternary statements. Tests like these should make them reconcider:
if vs switch
if switch indexed ternary

Here is a list of JavaScript tests that you might find useful or surprising.

…now get back to work!