Identical functions are referenced (in most implementations). This means that identical code gets a reference to the existing version, not a new function.

Hm. Quite hard to verify this, but I've succeeded in disproving it for 3 browser-based implementations and 2 standalone.

If you load the following into a browser, and click the "allocate array" link, and check the memory consumption when the "Hi there" alert is displayed, you'll see the memory consumed by an array of 10000 references to a single function.

<html> <head> <title>Javascript test</title> <script type='text/javascript'> var func = function () { return 12345; }; var size = 100000; var array; function allocateArray() { array = new Array( size ); for( var i = 0; i < size; i++ ) { array[ i ] = func; } alert( 'Hi there' ); } function populateArray() { for( var i=0; i < size; i++ ) { array[ i ] = function () { return 12345; }; } alert( "How's it goin'?" ); } </script> </head> <body> <a href=# onclick=allocateArray()>Allocate array</a> <a href=# onclick=populateArray()>Populate array</a> </body> </html>

If you then click the "populate array" link, and again check the memory when the alert is shown, you'll see the memory consumed when those function references are replaced by references to an "identical" function that's declared in-line.

If the JS interpreter was intelligent enough to recognise that the implementations are identical and somehow 'contant fold' them, there would be no increase in the memory consumption. The reality is, it at least quadruples in every implementation I've tested.

BTW. It's really not a good idea to iterate arrays using for( ... in ..) as you do in Re^6: How could we "implement" Open Classes in Perl ?. See the highlighted box here for details.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^9: How could we "implement" Open Classes in Perl ? by BrowserUk
in thread How could we "implement" Open Classes in Perl ? by Alien

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.