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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:51:17+00:00 2026-06-09T22:51:17+00:00

I can declare methods of an object in two ways: The first way uses

  • 0

I can declare methods of an object in two ways:

The first way uses the self=this idiom.

function SelfIdiomExample(name){
    var self = this;

    self.sayHello = function (name){
         alert("Hello, "+name);
    }
}

Which is useful when you need a reference to the object in a method (for example if the method will be passed as a callback). The other way is to do it by modifying the prototype:

function PrototypeModExample(){
   //pass
}

PrototypeModExample.prototype.sayHello = function(name){
   alert("Hello, "+name);
}

Both have the same result:

var sieg = new SelfIdiomExample();
var pmeg = new PrototypeModExample();

sieg.sayHello("Barnaby");
pmeg.sayHello("Josephine");

While I understand the use case for the self=this idiom, I am wondering:

Is there a performance penalty to using the methods created in the constructor versus methods created on the prototype?

  • 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-09T22:51:19+00:00Added an answer on June 9, 2026 at 10:51 pm

    Well this here:

    var self = this;
    

    Is not something has performance implications at all. It’s wicked fast as it’s simply accessing a local variable. Even from nested funcitons, this is a very fast operation in JavaScript.

    However, methods created in the constructor versus methods created on the prototype has a huge performance difference.

    In this example:

    var PrototypeModExample = function(){
      this.name = "Joe";
    };
    
    PrototypeModExample.prototype.sayHello = function(){
       alert("Hello, " + this.name);
    };
    
    var a = new PrototypeModExample();
    var b = new PrototypeModExample();
    console.log(a.sayHello === b.sayHello); // true
    

    Every instance of the constructor gets access to the same function objects. Which can be proved by using the === operator to compare the function objects on two instances. It will only return true when they are the same object. So globally we now have 2 instances, but they share one function object for the implementation of the sayHello method. This means that function is already setup and created when you want to make a new instance.

    In other words, for all instance obj.sayHello points to the same function object, which was created before any instances existed at all.


    But this on the the other hand:

    function SelfIdiomExample(name){
        var self = this;
        this.name = "Joe";
    
        this.sayHello = function(){
             alert("Hello, " + self.name);
        }
    }
    
    var a = new SelfIdiomExample();
    var b = new SelfIdiomExample();
    console.log(a.sayHello === b.sayHello); // false
    

    Works differently. Now we see that the === comparison is false. That’s because a new function object was created for each instance. Creating this function takes time (it needs to be parsed) and memory (this unique version of that function needs to be stored). So when creating lots of these instances, this method will be both slower and consume more memory.

    In other words, for all instance obj.sayHello points to a unique function object which was created when the instance itself was created.


    So usually, the prototype method is preferred. Especially in cases where a large number of instance might exist, since each instance can share function objects for it’s methods.

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

Sidebar

Related Questions

I'm evaluating selfish and am wondering how I can declare private methods/fields?
how can declare a 2nd or multidimensional array without first size in c++? class
Is there any way can declare a bean in just like JSP UseBean in
Can I declare a method in an object as both a static and non-static
How can I declare a method with keyword arguments just like rails do. some
How i can declare a global method in delphi prism using the __Global class?
In C#, how can I declare a method in an interface to return an
How do you declare an Objective-C static method that can be called without a
I can declare private static member variables in a class, but what does it
I can: declare @idOrder int set @idOrder = 21319 I want: declare @idOrder int

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.