Functional STDEV

Benchmark created by Anon on


Setup

var reduce = function(func, init, xs) {
          return xs.reduce(func, init);
        };
    
        var curry = function(fn, fnLength) {
          fnLength = fnLength || fn.length;
          return function() {
            var suppliedArgs = Array.prototype.slice.call(arguments);
            if (suppliedArgs.length >= fn.length) {
              return fn.apply(this, suppliedArgs);
            } else if (!suppliedArgs.length) {
              return fn;
            } else {
              return curry(fn.bind.apply(fn, [this].concat(suppliedArgs)), fnLength - suppliedArgs.length);
            }
          };
        };
    
        var map = curry(function(func, xs) {
          return xs.map(func);
        });
    
        var compose = function() {
          var funcs = arguments;
          return function() {
            var args = arguments;
            for (var i = funcs.length; i-- > 0;) {
              args = [funcs[i].apply(this, args)];
            }
            return args[0];
          };
        };
    
        var square = function(x) {
          return x * x;
        };
    
        var add = function(a, b) {
          return a + b;
        };
    
        var sum = function(s) {
          return reduce(add, 0, s);
        };
    
        var length = function(xs) {
          return xs.length;
        };
    
        var mean = function(xs) {
          return sum(xs) / length(xs);
        };
    
        var squaredDeviations1 = function(xs) {
          return map(function(x) {
            return square(x - mean(xs));
          }, xs);
        };
    
        var squaredDeviations2 = function(xs) {
          var avg = mean(xs);
          return map(function(x) {
            return square(x - avg);
          }, xs);
        };
    
        var set = [];
        for (var i = 1; i < 1001; i++) {
          set.push(i);
        }
    
        var stdev1 = compose(Math.sqrt, mean, squaredDeviations1);
        var stdev2 = compose(Math.sqrt, mean, squaredDeviations2);

Test runner

Ready to run.

Testing in
TestOps/sec
No local var
stdev1(set);
ready
With local var
stdev2(set);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.