Finance

Benchmark created on


Setup

const Old = (a, p, i) => {
    let c = a
      , o = 0
      , h = 0;
    for (; c > 0; ) {
        let P = parseFloat(p) / 100 / 12 * parseFloat(c);
        if (P > parseFloat(i))
            return {
                paymentTooLow: !0
            };
        let g = parseFloat(i) - parseFloat(P);
        c = parseFloat(c) - parseFloat(g),
        h += P,
        o++
    }
}

function New(
  principal,
  interest,
  monthly
) {
  let monthlyInterestRate = interest / 12;

  let totalMonthlyPayment = monthly;

  let months =
    Math.log(
      totalMonthlyPayment /
        (totalMonthlyPayment - monthlyInterestRate * principal)
    ) / Math.log(1 + monthlyInterestRate);

  let totalPayments = months * totalMonthlyPayment;

  let totalInterest = totalPayments - principal;

  return {
    months,
    totalInterest,
  };
}

Test runner

Ready to run.

Testing in
TestOps/sec
Old
Old(10000,17,142)
ready
New
New(10000,0.17,142)
ready

Revisions

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