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

  • Home
  • SEARCH
  • 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 991091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:03:05+00:00 2026-05-16T06:03:05+00:00

I read here (Douglas Crockford) using prototype operator to add methods to Javascript classes

  • 0

I read here (Douglas Crockford) using prototype operator to add methods to Javascript classes saves also memory.

Then I read in this John Resig’s article “Instantiating a function with a bunch of prototype properties is very, very, fast“, but is he talking about using prototype in the standard way, or is he talking about his specific example in his article?

For example, is creating this object:

function Class1()
{
   this.showMsg = function(string) { alert(string); }
}
var c = new Class1();
c.showMsg();

slower than creating this object, then?

function Class1() {}
Class1.prototype.showMsg = function(string) { alert(string); }
var c = new Class1();
c.showMsg();

P.S.

I know prototype is used to create inheritance and singleton object etc. But this question does not have anyhting to do with these subjects.


EDIT: to whom it might be interested also in performance comparison between a JS object and a JS static objet can read this answer below. Static object are definitely faster, obviously they can be usued only when you don’t need more than one instance of the object.

  • 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-16T06:03:06+00:00Added an answer on May 16, 2026 at 6:03 am

    Edit in 2021:

    This question was asked in 2010 when class was not available in JS. Nowadays, class has been so optimized that there is no excuse not to use it. If you need to use new, use class. But back in 2010 you had two options when binding methods to their object constructors — one was to bind functions inside the function constructor using this and the other was to bind them outside the constructor using prototype. @MarcoDemaio’s question has very concise examples. When class was added to JS, early implementations were close in performance, but usually slower. That’s not remotely true anymore. Just use class. I can think of no reason to use prototype today.


    It was an interesting question, so I ran some very simple tests (I should have restarted my browsers to clear out the memory, but I didn’t; take this for what it’s worth). It looks like at least on Safari and Firefox, prototype runs significantly faster [edit: not 20x as stated earlier]. I’m sure a real-world test with fully-featured objects would be a better comparison. The code I ran was this (I ran the tests several times, separately):

    var X,Y, x,y, i, intNow;
    
    X = function() {};
    X.prototype.message = function(s) { var mymessage = s + "";}
    X.prototype.addition = function(i,j) { return (i *2 + j * 2) / 2; }
    
    Y = function() {
      this.message = function(s) { var mymessage = s + "";}
      this.addition = function(i,j) { return (i *2 + j * 2) / 2; }
    };
    
    
    intNow = (new Date()).getTime();
    for (i = 0; i < 10000000; i++) {
      y = new Y();
      y.message('hi');
      y.addition(i,2)
    }
    console.log((new Date()).getTime() - intNow); //FF=5206ms; Safari=1554
    
    intNow = (new Date()).getTime();
    for (i = 0; i < 10000000; i++) {
      x = new X();
      x.message('hi');
      x.addition(i,2)
    }
    console.log((new Date()).getTime() - intNow);//FF=3894ms;Safari=606

    It’s a real shame, because I really hate using prototype. I like my object code to be self-encapsulated, and not allowed to drift. I guess when speed matters, though, I don’t have a choice. Darn.

    [Edit] Many thanks to @Kevin who pointed out my previous code was wrong, giving a huge boost to the reported speed of the prototype method. After fixing, prototype is still around significantly faster, but the difference is not as enormous.

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

Sidebar

Related Questions

I habe read here about defining method for a Javascript class Advantages of using
I read here about std::auto_ptr<>::operator= Notice however that the left-hand side object is not
As I read here , the VMT also contains a number of magic fields
I've read here and other places that when iterating a std::vector using indexes you
I read here Learn C Before Objective-C? Usually I then replace some Obj-C code
I've read here that editing a file in-place is not possible since it's OS
I have read here http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/ about how you probably shouldn't be trying to un-flatten
SO I read here In Entity Framework, getting the value of an identity column
Possible Duplicate: Search-like highlighting in NSTextView I thought i read here months ago that
I'm trying to set my entirely table row as an anchor. I've read here

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.