Smooth data (v3)

Revision 3 of this benchmark created on


Preparation HTML

<script>
if (!Array.prototype.reduce) {
  Array.prototype.reduce = function reduce(accumulator){
    if (this===null || this===undefined) throw new TypeError("Object is null or undefined");
    var i = 0, l = this.length >> 0, curr;
 
    if(typeof accumulator !== "function") // ES5 : "If IsCallable(callbackfn) is false, throw a TypeError exception."
      throw new TypeError("First argument is not callable");
 
    if(arguments.length < 2) {
      if (l === 0) throw new TypeError("Array length is 0 and no second argument");
      curr = this[0];
      i = 1; // start accumulating at the second element
    }
    else
      curr = arguments[1];
 
    while (i < l) {
      if(i in this) curr = accumulator.call(undefined, curr, this[i], i, this);
      ++i;
    }
 
    return curr;
  };
}
  function fake_data() {
    if (console && console.time) console.time("create data took")
    var d = [];
    var r;
    var start = new Date(2012, 0, 1);
    var end = new Date();
    var places = ["Bahrenfeld", "Altona", "Rotherbaum", "Niendorf", "Eimbüttel"];
    var starttime = start.getTime();
    var timediff = end.getTime() - starttime;
    var places_length = places.length;
    for (var i = 0; i < 1E4; i++) {
      r = Math.random();
      d.push({
        date: starttime + Math.random() * timediff,
        place: places[Math.round(r * places_length)],
        rating: r * 100
      });
    }
    if (console && console.timeEnd) console.timeEnd("create data took")
    return d;
  }
  var data = fake_data();
</script>

Setup

var smooth = function(values, max_values) {
        var window_length = Math.floor(values.length / max_values);
        var window, window_reduced, smoothed_value = [];
        for (var i = 0; i < max_values; i++) {
                window = values.slice(i * window_length, (i + 1) * window_length);
                window_reduced = window.reduce(function(previous, current) {
                        return [previous[0] + current[0] / window_length, previous[1] + current[1] / window_length];
                });
                smoothed_value.push(window_reduced);
        }
        return smoothed_value;
    }
    
    var smooth2 = function(values, max_values) {
        var window_length = Math.floor(values.length / max_values);
        var window, window_reduced, smoothed_value = [];
        var x=0, y=0;
        for (var i = 0; i < max_values; i++) {
                window = values.slice(i * window_length, (i + 1) * window_length);
                x = y = 0;
                for(var j = 0; j<window_length; j++){
                        x += window[j][0];
                        y += window[j][1];
                }
                smoothed_value.push([x,y]);
        }
        return smoothed_value;
    }
    var smooth3 = function(values, max_values) {
        var window_length = Math.floor(values.length / max_values);
        var window, window_reduced, smoothed_value = new Array(max_values);
        var x=0, y=0;
        for (var i = 0; i < max_values; i++) {
                window = values.slice(i * window_length, (i + 1) * window_length);
                x = y = 0;
                for(var j = 0; j<window_length; j++){
                        x += window[j][0];
                        y += window[j][1];
                }
                smoothed_value[i] = [x,y];
        }
        return smoothed_value;
    }
    
    
    var smooth4 = function(values, max_values) {
        var values_length = values.length;
        var window_length = Math.floor(values_length / max_values);
        var smoothed_value = new Array(max_values);
        var x=0, y=0;
        for (var i = 0; i < values_length; i++) {
                x += values[i][0];
                y += values[i][1];
                if(i%window_length === 0){
                        smoothed_value[i] = [x,y];
                        x = y = 0;
                }
        }
        return smoothed_value;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
smooth 1
smooth(data, 1000);
ready
smooth2
smooth2(data, 1000);
ready
smooth3
smooth3(data, 1000);
ready
smooth4
smooth4(data, 1000);
ready

Revisions

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