Do I like functional programming? Yes.
Is this particularly readable and self-explanatory, after all the optimizations? Meh.
Are there still pointless function calls and variable allocations? Basically.
But since I am in fact obsessed with using const so I know I'm not mutating this number later in the use site, I will extract this loop and assign to const:
/* IN THE ROOT SCOPE, NOT IN THE FUNCTION!!! */constl3s=(nums:number[])=>{letsum=0for(constnumofnums){sum+=3*curr**2}returnsum}/* elsewhere... */constsum=l3s(nums)
While using mapfilter and reduce is great for readability. Using a for loop is the fastest of all. So if your looking to optimize as much as possible i would go with a for loop.
Do I like functional programming? Yes.
Is this particularly readable and self-explanatory, after all the optimizations? Meh.
Are there still pointless function calls and variable allocations? Basically.
So if I will at all concern myself with optimizing this case I'll just go to the following, which doesn't at all lack readability:
But since I am in fact obsessed with using
const
so I know I'm not mutating this number later in the use site, I will extract this loop and assign to const:While using
map
filter
andreduce
is great for readability. Using a for loop is the fastest of all. So if your looking to optimize as much as possible i would go with a for loop.In Chrome 71 level2Sum is still 88% slower than a C-style for loop.
jsperf.com/chained-maps-vs-for-loop
In Edge 42 the level3Sum is just as slow as the original chained maps.
Oh, man. Indeed, that does look pretty messy. 😬