jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
The purpose of this test is to validate whether nested If-ElseIf conditions perform equally or more optimal than Switch statements and to compare my local test results against JSPerf's performance outcome.
According to local tests performed on my machine, it appears that the nested If-ElseIF conditions may perform the fastest but will eventually become sluggish as the 'variable-to-test' is tested against each conditional 'ElseIf' branch to find it's match.
In comparison, the 'Switch' statement maintains a consistent, favorable performance outcome whether testing against one 'case' or multiple 'case' branches. Lastly, the Multiple 'If' conditions resulted in the slowest performance of the three tested.
In conclusion (and my opinion), the 'Switch' statement performs very robust and provides a great value when evaluating against a long list of conditions. However, it may not always perform as equal as the 'If-ElseIf' conditional tests involving a short number of cases (e.g.. one to three nested conditions). Additionally, the 'Switch' statement offers a cleaner, visual style and structure in the code layout which can be beneficial to the code maintenance when evaluating a large number of conditions.
Ready to run.
Test | Ops/sec | |
---|---|---|
IF-Else-IF |
| ready |
SWITCH |
| ready |
Multiple IF conditions |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.