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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:36:05+00:00 2026-05-21T16:36:05+00:00

I read a lot of JavaScript code and see lots of different styles of

  • 0

I read a lot of JavaScript code and see lots of different styles of making so-called Classes. I’m developing a light-weight DOMish class which contains the bare minimum for my template script, running on Node.JS, (which transforms a DOMish instance into another DOMish instance using JSON and then serializes it to HTML, caching the original DOMish instance and cloning it per template-request).

Above is all irrelevant to my question. After reading http://www.phpied.com/3-ways-to-define-a-javascript-class/, Section 1.2. Methods defined internally

A drawback of 1.1. is that the method getInfo() is recreated every time you create a new object.

The way of defining classes, as described in Section 1.1, is (slightly modified to reflect to my own case, using local/private variables):

function Apple (type) {
    var that = this;

    // local-variable
    var color = 'red';

    // local-(extern)-function
    var infoProvider = function() { // inner-function
        // note the danger of this and that!
        return that.color + ' ' + that.type + ' ' + this.type;
    };

    this.__defineGetter__("type", function() { return type; });

    this.getInfo = function(otherContext) {
        return infoProvider.call(otherContext); // other "this" scope
    };
}

var apple = new Apple('iPod');
apple.getInfo({type: 'music player'}); // 'red iPod music player'

I happen to use the same style, as the function now has access to local/private variables and functions, defined inside the constructor. But the sentence “[The function] is recreated every time you create a new object.” scares me (performance wise)! As I’m using Node, which uses V8, I always thought that functions are only created once and cached some-how, when called on different objects, using only a different context (the thises and the thats).

Should I be scared of “recreation”? How bad is it compared to prototype-based functions? Or is this purely aesthetic (people love keeping stuff together, in the constructor, VS, people love prototypes)?

  • 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-21T16:36:06+00:00Added an answer on May 21, 2026 at 4:36 pm

    Although V8 does indeed cache many things, this isn’t one of those cases, as can be demonstrated by the following code:

    > function foo(){ this.bar = function(){ return this.x; }; this.x = Math.random(); }
    > var ary = [];
    > for(var i=0; i<1000000; i++){ ary.push(new foo()); }
    

    the above uses 144MB of memory.

    > function foo(){ this.x = Math.random(); }
    > foo.prototype.bar = function(){ return this.x; }
    > var ary = [];
    > for(var i=0; i<1000000; i++){ ary.push(new foo()); }
    

    And this uses 68MB of memory.

    Note that the V8 source header at:

    http://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/compilation-cache.h?r=5284

    Seems to imply that the compilation of said function may indeed be cached :

    The compilation cache keeps shared
    function infos for compiled scripts
    and evals. The shared function infos
    are looked up using the source
    string as the key. For regular
    expressions the compilation data is
    cached.

    But the test shows that even if the compilation is compiled, new objects are still created, as if you were not creating new objects you would break JS 🙂

    EDIT

    A further test:

    function foo(){
        this.bar = function(){
            this.x = '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789';
            return this.x;
        };
        this.x = Math.random();
    }
    var ary = [];
    for(var i=0; i<1000000; i++){ ary.push(new foo()); }
    

    Uses 144MB of memory. Due to the fact that I added a 100 char string, if the function itself were not cached, we would be using an extra 100MB or so of memory.

    So the above indicates that yes, the function itself is cached. But it still needs to be represented by a new object.

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

Sidebar

Related Questions

I read a lot about cross-site scripting with Flash, Javascript etc. and also found
I have read a lot that LISP can redefine syntax on the fly, presumably
I've read a lot of people saying that some things shouldn't be written in
I've read a lot about the pros and cons of sizing with either relative
I have read a lot of popular standards manuals for open source PHP projects.
I have read a lot of articles on Dependency Injection as well as watched
I've read a lot recently about how writing multi-threaded apps is a huge pain
While developing my ASP.NET MVC, I have started to see the need for a
I used to work in JavaScript a lot and one thing that really bothered
I'm building some Python code to read and manipulate deeply nested dicts (ultimately for

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.