rational.js vs fraction.js vs ratio.js - subtract, multiply, add, divide

JavaScript performance comparison

Revision 22 of this test case created

Info

Comparing the performance of various rational number libraries for basic arithmetic.

Preparation code

<script src="https://rawgithub.com/infusion/Fraction.js/master/fraction.min.js"></script>
<script>
// Little hack for the problem of the same name
var InfusionFraction = Fraction;
</script>

<script src="http://acerix.github.com/rational.js/releases/rationaljs.alpha.js">
</script>
<script src="https://rawgithub.com/ekg/fraction.js/master/fraction.js"></script>
<script src="https://rawgithub.com/LarryBattle/Ratio.js/master/lib/Ratio-0.4.0.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var test_cases = [
      [462, 347],
      [1, 69],
      [-31, 30],
      [735, 666],
      [-323, 241]
    ];
   
    var float_1 = 1.0;
    var float_2 = 1.0;
   
    var rat_1 = rat.fromInteger(1);
    var rat_2 = rat.fromInteger(1);
   
    var bigrat_1 = bigrat.fromInteger(1);
    var bigrat_2 = bigrat.fromInteger(1);
   
    var rational_1 = new rational(1, 1);
    var rational_2 = new rational(1, 1);
   
    var ratio_1 = Ratio(1, 1);
    var ratio_2 = Ratio(1, 1);
   
    var frac_1 = new Fraction(1, 1);
    var frac_2 = new Fraction(1, 1);
   
    var infrac_1 = new InfusionFraction(1, 1);
    var infrac_2 = new InfusionFraction(1, 1);
   
   
    // test for accuracy
    for (var i in test_cases) {
   
        console.log('case: '+test_cases[i]);
   
       
        rational_1 = new rational(test_cases[i][0], test_cases[i][1]);
        rational_2 = rational_2.sub(rational_1).mul(rational_1).add(rational_1).div(rational_1);
   
        console.log('rational: '+rational_2.toString());
       
       
        ratio_1 = Ratio(test_cases[i][0], test_cases[i][1]);
        ratio_2 = ratio_2.subtract(ratio_1).simplify().multiply(ratio_1).simplify().add(ratio_1).simplify().divide(ratio_1).simplify();
   
        console.log('Ratio: '+ratio_2.toString());
   
    }
   
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
rational.js - rat{}
for (var i in test_cases) {
  rat.set(rat_1, test_cases[i][0], test_cases[i][1]);
  rat.subtract(rat_2, rat_2, rat_1);
  rat.multiply(rat_2, rat_2, rat_1);
  rat.add(rat_2, rat_2, rat_1);
  rat.divide(rat_2, rat_2, rat_1);
}
pending…
fraction.js
for (var i in test_cases) {
  frac_1 = new Fraction(test_cases[i][0], test_cases[i][1]);
  frac_2 = frac_2.subtract(ratio_1).multiply(ratio_1).add(ratio_1).divide(ratio_1);
}
pending…
Ratio.js
for (var i in test_cases) {
  ratio_1 = Ratio(test_cases[i][0], test_cases[i][1]);
  ratio_2 = ratio_2.subtract(ratio_1).simplify().multiply(ratio_1).simplify().add(ratio_1).simplify().divide(ratio_1).simplify();
}
pending…
rational.js - bigrat{} (arbitrary size)
for (var i in test_cases) {
  bigrat_1 = bigrat.fromValues(test_cases[i][0], test_cases[i][1]);
  bigrat.subtract(bigrat_2, bigrat_2, bigrat_1);
  bigrat.multiply(bigrat_2, bigrat_2, bigrat_1);
  bigrat.add(bigrat_2, bigrat_2, bigrat_1);
  bigrat.divide(bigrat_2, bigrat_2, bigrat_1);
}
pending…
rational.js - rational{} (chainable)
for (var i in test_cases) {
  rational_1 = new rational(test_cases[i][0], test_cases[i][1]);
  rational_2 = rational_2.sub(rational_1).mul(rational_1).add(rational_1).div(rational_1);
}
pending…
Fraction.js - infusion
for (var i in test_cases) {
  infrac_1.set(test_cases[i][0], test_cases[i][1]);
  infrac_2.sub(infrac_1).mul(infrac_1).add(infrac_1).div(infrac_1);
}
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