Today I came across a question How to sum the values of a JavaScript object?. I noticed that using latest ECMAScript features the problem can be solved with shorter, simpler and (IMHO) easier‑to‑understand code, so I decided to post an answer.
I expected that people will appreciate my effort to provide a more up‑to‑date answer, but instead I got 2 downvotes (that was in the moment of writing this question, now it has score of 0). Someone also posted a comment complaining that:
- It's not worthy to include a 60K library (if we're talking about babel-polyfill, it's actually only 32.6K gzipped) just to have
Object.values()
. - It's 4 times slower than solution using
for
loop.
I don't quite agree with these arguments.
- You have to use Babel anyway if you want to use any new ECMAScript features and still support older browsers. And I'm not talking only about ES 2016/2017. For example IE 11, which still is still used by many people, supports only 11% of ES 6 (according to kangax ES compatibility table).
- I think that "4 times slower" isn't that much unless you're working with a huge amount of data (which doesn't happen to most people). Also, even if it's 4 times slower now, it doesn't mean that JavaScript engines developers won't improve performance of the
reduce()
method in the future.
Don't get me wrong, I'm not trying to prove that someone is not right. I just want to be helpful to the community. If answers like the one I posted aren't welcome, I can understand that.