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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:57:16+00:00 2026-05-23T22:57:16+00:00

So when I make a library, I usually do it in this fashion: var

  • 0

So when I make a library, I usually do it in this fashion:

var myLib = (function() {
    return {
        publicProperty: 'test',
        publicMethod: function() {
            console.log('public function');
        },
        anotherMethod: function() { //... },
        // .. many more public methods
    };
}());

I had overheard that creating libraries is faster and/or uses less memory for initialization if you write it like this:

var MyLib = function() {
   this.publicProperty = 'test';
};

MyLib.prototype = {
    publicMethod: function() {
        console.log('public method');
    },
    anotherMethod: function() { //... },
    // ... many more public methods
};

myLib = new MyLib();

Does one initialize faster than the other? Does my question even make sense? I assume that these accomplish the same task (that task being I go and use myLib.publicMethod() somewhere else in my code on docready). Thanks!

  • 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-23T22:57:17+00:00Added an answer on May 23, 2026 at 10:57 pm

    The two scripts produce different results.

    In the first you create the object myLib with three (or more) properties. Its prototype is Object.prototype. The point is you have made one object.

    In the second you create a global variable called publicProperty. This is probably not what you wanted. (EDIT: OP Question Corrected, this is no longer an issue.)

    Still, assuming you meant to create an object with methods in the prototype, your code is probably slower on a naive JavaScript engine because calling methods in the prototype requires a traversal of the link from the object to the prototype.

    Now if your intent is not to make a single but to make many instances of myLib then you will save a lot of memory by putting methods in the prototype, because you will have one copy of each function. You don’t want thousands of copies of methods. 🙂

    EDIT TO ADDRESS OP QUESTION:

    Regarding the two approaches and how to make the first one “faster”:

    Theoretically you have a space-time tradeoff. By putting the methods directly in the object you don’t have the prototype chain lookup. But the downside is that every instance has its own copy of the methods. If you are making multiple instances, you should define each of the methods once and place them in a prototype. Modern (V8-class) JavaScript engines are pretty good with this kind of stuff, so you should not be looking for optimize speed by packing methods into the object. Now, for singletons, go ahead and fill up the object. Another approach is to put all the methods in myLib itself, and create instances with Object.create(myLib). This makes myLib itself be the prototype for all the “instances” and is generally considered good form (at least if you follow the “Good Parts” suggestions, which I generally do).

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

Sidebar

Related Questions

I'm trying to make a python binding for the this library: http://code.google.com/p/hosterslib/ . I'm
Using JNA, the documentation says: Make your target library available to your Java program.
I just found out about ie7-js ; IE7 is a JavaScript library to make
I have a project in my IDE. I need to make a shared library
The Microsoft AJAX Library Preview 6 and jQuery provide several ways to make the
I am building an application as a library, but to make sure I can
I have a C++ app and looking for a library that would make it
I am working on a C++ library. Ultimately, I would like to make it
I'm using Code::Blocks to compile a shared library on Ubuntu. When I make a
This is my first time trying to use the XMLRPC::Client library to interact with

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.