This document discusses techniques for improving JavaScript performance, including:
- Using for loops instead of foreach loops for improved speed when iterating over arrays
- Memoization to cache function results and avoid repeated computations
- Lazy evaluation with libraries like underscore.js to defer computation until results are needed
- Parallelization using functional programming techniques like liftA3 to run operations concurrently
- Compile-time optimizations like loop fusion that rearrange code to reduce operations
The document emphasizes that functional programming allows optimizations like these through composability and avoiding side effects.
35. var result = [];
for(var i=0, len=arr.length; i < len; i++) {
result.push(doSomething(arr[i]));
}
return result;
For loops are faster!
arr.map(doSomething);
45. ”you are not allowed to care about the performance of * unless you
concatenate all your javascript, have it at the bottom, minify your css and
js, gzip all your assets, and losslessly compress all your images. If you
aren’t getting 90+ Page Speed scores, it’s way too early to be thinking
about selector optimization.”
64. Shortcut Fusion
// g :: forall b. (t -> b -> b) -> b -> b
reduce(c, n, build(g)) = g(c, n)
65. Shortcut Fusion
//build :: (forall b. (a -> b -> b) -> b -> b) -> [a]
var build = function(g){
return g(concat, []);
}
//+ map :: (a -> b) -> [a] -> [b]
var map = curry(function(f, xs){
return build(function(c, n){
return reduce(function(acc, x){ return c(f(x), acc); }, n, xs);
});
});
66. Shortcut Fusion
var sum = reduce(add, 0);
var sqr = function(x) {return x * x }
var sumSqs = compose(sum, map(sqr))
// reduce(function(acc, x){ return add(sqr(x), acc) }, 0);
67. Shortcut Fusion
var sum = reduce(add, 0);
var sqr = function(x) {return x * x }
var sumSqs = function(xs) {
return chain(xs).map(sqr).sum().value()
// chain(xs).reduce(function(acc, x){ return add(sqr(x), acc) }, 0).value();
}
68. Good Producers
• List comprehensions
• concat
• foldr
• map
• take
• filter
• partition
• head
• and, or, any, all
• sequence
• sortBy
• zip
• zipWith
• List comprehensions
• Explicit lists
• cons
• concat
• map
• take
• filter
• iterate, repeat
• repeat
• zip
• zipWith
Good Consumers