Jsperf for Fast.js, Lodash, Native
jsperf.com
jsperf.com
Even in cases where this lib soundly beats native, I think I'd prefer the native variants for their fluency. Combinations of filter, map and reduce are extremely powerful ways to manipulate arrays, and I find that non-native array functions discourage this pattern due to lack of fluency. Maybe they could implement a chain function like underscore?
``` var startWith123 = _.partial(_.concat(1, 2, 3)); ```
and you can more easily pass them around without having to create anonymous functions, which is a lot more fluid in my opinion.
var mapOverNums = _.partial(_.map, [1,2,3,4,5]);
mapOverNums(function(n) { return n * n; }); // Square nums
mapOverNums(function(n) { return Math.abs(n); }); // Absolute values
...can be reproduced with... var mapOverNums = Array.prototype.map.bind([1,2,3,4,5]);
Without a thrush-esque operator (-> in Clojure, |> in F#, etc.), full first class functions make chains of computation unpleasant to read. e.g. "I want the sum of the square roots of all numbers greater than 100 in this list". nums.
filter(function(n) { return n > 100; }).
map(Math.sqrt).
reduce(function(acc, val) { return acc + val; }, 0); var nums = Array.apply(0, Array(300)).map(function(x, idx) {
return idx;
});
var filter = ramda.filter;
var map = ramda.map;
var reduce = ramda.reduce;
var compose = ramda.compose;
var gt100pred = function(x) {
return x > 100;
};
var add = function(a, b) {
return a + b;
};
var result = nums
.filter(gt100pred)
.map(Math.sqrt)
.reduce(add, 0);
var gt100 = filter(gt100pred);
var sqrt = map(Math.sqrt);
var accum = reduce(add, 0);
var result = compose(accum, sqrt, gt100)(nums);
=============with the above your code does look shorter. but there is one big plus with the latter that not only can you pass around the predicate but the operation in itself can be passed around(gt100 is reusable etc..). (ramda actually works like a functional library wherein the function/predicate is the first argument and the data structure is the last. this makes partial function application and currying work much better)
also when we do away with the boilerplate the final thing looks like this:
// "I want the sum of the square roots
// of all numbers greater than 100 in this list".
var result = nums
.filter(gt100pred)
.map(Math.sqrt)
.reduce(add, 0);
vs // think of it as pipelining from right to left.
// get any number > 100, sqrt it, then accumulate it.
var result = compose(accum, sqrt, gt100)(nums);
and if this was haskell: foldl (+) 0 . map sqrt . filter (> 100) $ [0..300] pluck = curry(reverse(object.get));
var names = people.map(pluck('name'));
...but they weren't having it. :-) As much as I like writing Haskell (and believe me, I do) JS can't do it without heavy library support. You may be better off writing LiveScript or Fey at that point.Check out the "first steps" tutorial[1] solving Euler #1
0: http://www.purescript.org/ 1: http://www.purescript.org/posts/First-Steps/
haha man i dont know how many time ive written a reverse/flip function in our codebase and then erased it an hour later cause i know i would be called out on it lol
A pity that for the average call count for this functions, the browser will expend much, much more loading and evaluating this code, rather than executing the function.
Are you testing a nightly build?
indexOf and lastIndex of are only a few percent faster.
forEach is 1/4 the speed of lodash and native.
bind is 1/6 the speed of native (though 2x faster than lodash).
map is 1/4 the speed of lodash and native.
reduce is 1/7 as the speed of native, though faster than lodash.
concat is about half as fast as native.
They've changed something drastically recently in SpiderMonkey. When I tried my application for FOS on desktop Firefox Nightly performance was much better than few months ago.
UPDATE: the revisions work - http://jsperf.com/fast-vs-lodash/3
Even if end-developers don't take this up it may end up benefiting framework developers like Sizzle, jQuery and others who have a strong test suite and can use this to benchmark their performance ceilings and push them further.
Chrome / Win7