Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8318875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:07:46+00:00 2026-06-08T22:07:46+00:00

I wrote three javascript functions which works the same task. They all provide the

  • 0

I wrote three javascript functions which works the same task. They all provide the same result but I am wondering which one of them is faster and most scalable?

Here are my functions
first one

function odds(n, p) {
    var acc = 1;
    for (var i = 0; i < n; i++) {
        acc *= (n - i) / (p - i);
    }
    return acc;
}

second one

function odds(n, p) {
    if (n == 0) {
        return 1;
    } else {
        return (n / p) * odds(n - 1, p - 1);
    }
}

third one

var odds = (function () {
    var odds1 = function(n, p, acc) {
        if(n == 0) {
            return acc;
        } else {
            return odds1(n - 1, p - 1, (n / p) * acc);
        }
    }
    return function(n, p) {
        return odds1(n, p, 1);
    }  
}());
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:07:47+00:00Added an answer on June 8, 2026 at 10:07 pm

    You must test to answer questions like this, but it’s also generally true that an internal loop is nearly always faster than recursion in javascript. Test harnesses like jsPerf can be very, very helpful for giving you real data.

    I built a jsPerf for your three cases here: http://jsperf.com/oddsflavors. For the parmeters I picked (I wasn’t sure what parameters would be most important to you), it shows your first code option (the one without recursion) to be about twice as fast as the second one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote one script which gets an image url through JSONP, and then I
I have a series of anonymous jQuery/javascript functions for click events which have to
I am using a bookmarklet to load an html page which all works great,
I am wondering if there is a better way to write this JavaScript (Jquery)
Need help with a query that I wrote: I have three tables Company id
I am trying to test a website which contains alot of javascript. Javascript does
I've had a hard time debugging a news ticker - which I wrote from
Scenario: I have a Javascript-generated web page that someone else wrote a long time
There is a JavaScript function, of which I have zero control of the code,
I'm trying to write some simple chat client in javascript, but I need a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.