Well, yeah, because most JS frameworks aren't writing about how to sum the squares of 32 million floating point values.
Most JS use-cases are about front-end UIs which both generally don't include huge data calculations, and are generally IO-bound, not CPU-bound, anyway: the performance bottlenecks front-end UIs almost always come from network requests or DOM operations, and not from the speed of list manipulation operations.
In the vast majority of cases, the readability/maintainability concerns are more important than the performance implications, which is why I prefer .map/.reduce and other higher-order friends, over simple for loops (or .forEach loops).
Most JS use-cases are about front-end UIs which both generally don't include huge data calculations
JS and the browser is increasingly used as an app platform. You can record audio and even video. I myself wrote some code recently to perform RMS audio normalization on the client, which involved doing the sum of squares of quite a large number floating point vlaues (don't think it was 32 million though).
I'm quite sure it's not because your websites are iterating over 32 million floating point numbers inefficiently. A lot of it is DOM rendering - reflows, expensive CSS animations, etc - which, yeah, is CPU-based, but from the perspective of Javascript code, it's IO.
AFAIK, it's very rarely the issue that the JS thread is overloaded.
Yeah, I clarified a bit on a different part of the thread:
A lot of it is DOM rendering - reflows, expensive CSS animations, etc - which, yeah, is CPU-based, but from the perspective of Javascript code, it's IO.
From the machine's perspective, yeah, it's CPU work, but my original point is that the problem is pretty much never that the JS thread is overloaded, the efficiency of array iteration is generally not the issue.
Honestly I'm just sick and tired of people writing obviously slow code and then justifying it by saying "but my application is IO bound not CPU bound".
In the vast majority of cases, the readability/maintainability concerns are more important than the performance implications, which is why I prefer .map/.reduce and other higher-order friends, over simple for loops (or .forEach loops).
so why the hell are every friggin website on earth running at 10fps !
Assuming it is a genuine question and not a rhetorical one, here are a few reasons:
Because even a single synchronous piece of code can slow down the main thread and stall the whole UI. E.g., just synchronously reading or writing document.cookie in an onscroll handler might slow down UI.
Because you might have weird CSS (or JS changing CSS) that forces redraw when nothing changes on the page. Try opening Console > Rendering and select "Paint flashing". If you see a lot of stuff being redrawn (flashed) when it should not change, try simplifying your styles.
the performance bottlenecks front-end UIs almost always come from network requests or DOM operations, and not from the speed of list manipulation operations.
In the vast majority of cases, the readability/maintainability concerns are more important than the performance implications, which is why I prefer .map/.reduce and other higher-order friends, over simple for loops (or .forEach loops).
You really think that this:
var sum = values.map(x => x*x).
reduce( (total,num,index,array) => total+num,0.0);
is more readable than this:
var sum = 0.0;
for (var i = 0; i < values.length;i++){
var x = values[i];
sum += x*x;
}
I think their reduce code is badly written, but to the general point, yes, I think this is clearer:
values.map(x => x * x)
.reduce((a, b) => a + b)
Is it pretty much a moot point for this incredibly simple use-case? Yes, but as the complexity grows, the benefits of the functional style really show, compared to large for loop.
Yes I do. Don't you? No extra counter variable to keep track of, no generalized for loop that could be doing anything, no in-place mutation of variables. In fact the only way to read the second (longer) code quickly is to recognize that it's a particular common pattern - wouldn't it be better to actually give that pattern a name and pull out the common parts?
What I think is irrelevant. What I've seen is that most programmers don't parse the more complex expression as easily as the simpler one.
No extra counter variable to keep track of, no generalized for loop that could be doing anything, no in-place mutation of variables. I
No, but extra keywords to recognise (map, then reduce), extra concepts to learn (map/reduce in particular, multiple compound expressions), anonymous functions if you want to do anything non-trivial.
I don't see the first form as being harder to maintain.
What I've seen is that most programmers don't parse the more complex expression as easily as the simpler one.
I'd agree with that statement, but I suspect you're claiming that the more complex one is "simpler".
extra keywords to recognise (map, then reduce),
Not keywords, just functions. They behave like normal functions, and usually you can read their source code if you want to know what they do.
extra concepts to learn (map/reduce in particular, multiple compound expressions), anonymous functions if you want to do anything non-trivial.
I don't know what you're calling "multiple compound expressions". Both implementations use + and * operators with their normal mathematical meaning and a literal 0.0. In addition to that the map/reduce version only requires the reader to understand a normal expression made of function calls and an anonymous function (both very general constructs that you use again and again on a large codebase). The imperative version requires understanding a mutable variable, the for keyword, the ++ and += operators which are not standard mathematical things, the [i] operator which is not standard anywhere else either, and a {} block of ;-separated statements. In addition to just being a much bigger pile of concepts, half those things are special-case dedicated operators that can't be reused for much else (e.g. [i] is only for arrays, ++ is only for numbers).
They're both about as readable to each other as me.
You realise you didn't come out of the womb being able to read for loops, right? Just because it's typically taught to us first does not make it inherently more readable. I know basic functional constructs, so I know what map and reduce do.
Your original comment only mentioned readability. Maintainability of such a tiny piece of code is pointless to even talk about, though I’d nominally argue that the one with a shorter number of lines which isn’t mutating state is more maintainable.
Stop with the excuses, they're the reasons we don't have anywhere the nice things our hardware could afford us.
Most JS use-cases are about front-end UIs which both generally don't include huge data calculations
Maybe so, but they're still potentially one tab among dozens in the browser, which is one application among dozens. Stuff adds up. Performance matters. People are getting trained to not have the faintest clue or care about what goes under the hood. Right now, the JIT does a lot for them. But keep it up, and there will be nobody able to code a JIT.
readability/maintainability concerns
That's just a meme at this point. If people had a clue, they could read the better code just fine.
17
u/Retsam19 Apr 17 '19 edited Apr 17 '19
Well, yeah, because most JS frameworks aren't writing about how to sum the squares of 32 million floating point values.
Most JS use-cases are about front-end UIs which both generally don't include huge data calculations, and are generally IO-bound, not CPU-bound, anyway: the performance bottlenecks front-end UIs almost always come from network requests or DOM operations, and not from the speed of list manipulation operations.
In the vast majority of cases, the readability/maintainability concerns are more important than the performance implications, which is why I prefer
.map/.reduceand other higher-order friends, over simple for loops (or.forEachloops).