Hacker News new | ask | show | jobs
by daleharvey 4831 days ago
Often a good reason to use not so pretty code is for performance reasons, you didnt mention checking up on that so it would probably be prudent to do so (micro optimisations are totally valid for hot paths in libraries like jquery and browser performance is often very unintuitive)

Also if I was going to write it 'cleaner', I would find something like http://pastie.org/7118990 cleaner.

(but in essence I agree)

1 comments

Do ternary operators really make much of a performance difference compared to if-else statements? I'd imagine they're not implemented much differently when it comes down to the instruction level (you still have the conditional branch). If they do make a difference, I'd hope the browser makes the optimization. Nevertheless, as you mention, this may not be the case.
It's not about the actual number of operations the code compiles down to... it's about the number of bytes sent across the wire on initial page load. Among the most important elements of optimizing a webpage is minimizing the initial HTTP latency/download size. The jQuery core team is hyper-aware of all of these performance issues, and are doing everything possible to cut the gzipped/compressed size of their library. When you support a library that is literally run on the majority of the internet, these small improvements make a HUGE difference in the overall bandwidth consumption and load times of web applications.

I agree with you that use of the ternary operator, when not used properly, can obfuscate some code that could be simplified with if/else operators. But, jQuery has chosen to forgo readability for performance (and for good reason IMO).