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 7533619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:46:37+00:00 2026-05-30T05:46:37+00:00

Theoretical case. The function init () may not always be called on the page

  • 0

Theoretical case. The function init () may not always be called on the page. What I want to know is if there is a difference from a speed/efficiency point of view between the two scripts? Will the fact that Class is a singleton make a difference to loading speed/amount of processing?

Script 1:

// Class is a singleton.
var Class =
{
    myFunctionOne : function ()
    {
    }

    myFunctionTwo: function ()
    {
    }
}

function init ()
{
    //Do some fancy stuff

    Class.myFunctionOne();
    Class.myFunctionTwo();
}

Script 2:

function Class ()
{
    this.myFunctionOne = function ()
    {
    }

    this.myFunctionTwo = function ()
    {
    }
}

function init ()
{
    var myClass = new Class();

    //Do some fancy stuff

    myClass.myFunctionOne();
    myClass.myFunctionTwo();
}
  • 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-05-30T05:46:38+00:00Added an answer on May 30, 2026 at 5:46 am

    I would say yes, your second script has a performance advantage when init() is not being called on the page. Here’s why.

    In your first example, the object literal will be parsed each time the page is loaded:

    var Class =
    {
        myFunctionOne : function () { ... }
    }
    

    The Class object will be built, each key set to a function object. This overhead will be incurred regardless of whether these “methods” are ever actually called (though, it’s possible certain JavaScript compilers may implement optimizations here such as “Just-In-Time compiling”, so the actual perf gain may vary browser to browser)

    In your second example:

    function Class ()
    {
        this.myFunctionOne = function () { ... }
    }
    

    The myFunctionOne line is being run each time Class is being called, in your case through the new operator. This cost will be incurred each time Class is instantiated rather than once per page load. However you’ve stated this class is using a singleton pattern so you’ll only have one instance anyway.

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

Sidebar

Related Questions

I'm writing an Android app which receives data from a server. Theoretical there could
This is mostly a theoretical question I'm just very curious about. (I'm not trying
I have a very theoretical question: Is there a way to ban the use
Here is a pretty theoretical and abstract question: How Service-oriented architecture (SOA) differs from
I want to pass an object by smart-pointer reference to a function. The function
NOTE : I am asking for real world problem, not for theoretical purpose; see
A quite theoretical question this time. So I'm using this function in Eclipse: CsvReader
This is a theoretical question more than anything else. In the case of C#
By theoretical computer science topics, I am referring to things such as regular vs
This is a purely theoretical question. Given three simple classes: class Base { }

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.