smenjas / chemistry-explorer

A chemistry app built with vanilla JavaScript
https://smenjas.github.io/chemistry-explorer/
ISC License
1 stars 2 forks source link

Factor compareElement() out of compare(). #49

Closed smenjas closed 1 year ago

smenjas commented 1 year ago

This simplifies compare() by breaking duplicated logic into a helper function. It doesn't make sense to repeat the same decision tree for both the priority element and the rest.

This also seems faster than the previous version. Sorting all 4705 formulas in the database takes 25-50 ms now, whereas previously I was seeing times from 30 to over 100 ms occasionally. Perhaps compareElement() is more efficient than in the previous priority element path.

There are 16 fewer lines of code, and 1277 fewer bytes, at the expense of less descriptive debug output. The automated tests show identical behavior. I hope they're sufficiently thorough that I didn't introduce any regressions.