Linear Interpolation

JavaScript performance comparison

Revision 3 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    // Raw
    function linearInterpolation(min, max, k) {
      return min + (max - min) * k;
    }
   
    // Functional
    function linearInterpolationF(min, max) {
      var delta = max - min;
      return function(k) {
        return min + delta * k
      };
    }
   
    // OOP
    var LinearInterpolator = function(min,max) {
      this.min = min;
      this.delta = max - min;
    }
   
    LinearInterpolator.prototype = {
        interpolate: function(k) {
                return this.min + this.delta * k;
        }
    }
   
    var interpolations = 100;
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Raw
for (var i = 0; i < interpolations; i++) {
  linearInterpolation(10, 100, i / interpolations);
}
pending…
Functional
var interpolateFunctional = linearInterpolationF(10, 100);
for (var i = 0; i < interpolations; i++) {
  interpolateFunctional(i / interpolations);
}
pending…
OOP
var interpolateOOP = (new LinearInterpolator(10, 100)).interpolate;
for (var i = 0; i < interpolations; i++) {
  interpolateOOP(i / interpolations);
}
pending…
OOP (revised)
var oop = new LinearInterpolator(10, 100);
for (var i = 0; i < interpolations; i++) {
  oop.interpolate(i / interpolations);
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment