#447 enhancement
eno

String.interpret: reducing memory impact

Reported by eno | November 16th, 2008 @ 02:15 PM

Even String.interpret is not exactly well-documented, I take it is meant to interpret strings as strings and some invalid values (undefined and the like) as empty strings.

Now at least FF3's javascript creates a new string object on each invocation of String.interpret - even in case the object to intrepret is already a string. This is a bad thing - especially as FF fires up a callback at XMLHttpRequest's readyState 3 every 4 KByte, which makes applications with large AJAX responses terribly slow and memory consuming.

Instead of the current implementation:

interpret: function(value) {

return value == null ? '' : String(value);

},

String.interpret should behave along the lines of

interpret: function(value) {

return value == null ? '' :
  typeof(value) == "string" ? value : String(value);

},

or - as I would recommend -

interpret: function(value) {

return value == null ? '' :
  typeof(value) == "string" ? value : "" + value;

},

because in some strange circumstances in WebKit the string conversion of some string objects returned from a java applet returned Objects, and not strings.

(This might also fix #124)

Comments and changes to this ticket

  • Tobie Langel

    Tobie Langel November 16th, 2008 @ 08:15 PM

    • Assigned user set to “Tobie Langel”
    • State changed from “new” to “enhancement”

    Note that your last solution also creates a new, unneeded string object for type coercion.

    You might want to favor something like this: var EMPTY_STRING = ''; return value == null ? EMPTY_STRING : typeof(value) === "string" ? value : EMPTY_STRING + value;

    },

  • eno

    eno November 17th, 2008 @ 10:45 AM

    My gut feeling tells different: doesn't your solution create the EMPTY_STRING object on each invocation of String.interpret, whether it is needed or not?

  • Tobie Langel

    Tobie Langel November 17th, 2008 @ 10:47 AM

    Well, of course you'd want to store that in a closure.

  • Juriy Zaytsev

    Juriy Zaytsev November 17th, 2008 @ 02:11 PM

    because in some strange circumstances in WebKit the string conversion of some string objects returned from a java applet returned Objects, and not strings

    Would you happen to have a test case for this? By the way, it looks like +'' conversion is a bit faster than String one.

  • eno

    eno November 17th, 2008 @ 02:58 PM

    @tobie: yes, for sure ;-)

    @juriy: i can try to shrink our current applet to a testcase size and will post it here then. Pls check back tomorrow.

    @all: we found out that even changing String.interpret in one of the ways suggested here doesn't fix #124. That means that String.interpret() might not responsible for the huge memory impact when evaluation Ajax readyState 3 in Firefox. Which raises the question: who is responsible? The workaround in #214 (i.e. skipping readyState 3 completely) works fine here...

  • Tobie Langel

    Tobie Langel November 17th, 2008 @ 03:11 PM

    Yeah, I think the main issue with high memory consumption is the stupid way I designed the ajax response object. (A new instance is created every time).

    I think we should fix that.

  • Samuel Lebeau
  • eno

    eno November 17th, 2008 @ 07:07 PM

    @juriy:

    // Take this applet...

    public class Applet extends java.applet.Applet { public String str() { return "value"; } }

    // ...and this HTML

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    
    <object classid="java:Applet" height="1" id="applet" style="position:absolute; left: -1000px;" type="application/x-java-applet" width="1">
      <param name="codebase" value="/java" />
      <param name="mayscript" value="true" />
    </object>
    <script type="text/javascript" charset="utf-8">
      setTimeout(function() {
        var applet = document.applet;
        alert(applet.str() + "/" + typeof(applet.str()) + "/" + typeof(applet.str().toString()));
      }, 1000);
    </script>
    
    

    FF3 reports value/string/string, Safari 3 report value/object/object, where I would at least object the last object appeareance :)

    But this goes beyond the scope of this ticket; I only mentioned it, because val.toString() doesn't work, where "" + val works as expected. Having said that I then checked into String(val) which works as announced, i.e. returns a string.

  • Juriy Zaytsev

    Juriy Zaytsev November 17th, 2008 @ 07:20 PM

    Sorry, how would I point document to an applet? (I'm not familiar with objects's nor applet's)

  • eno

    eno November 17th, 2008 @ 08:15 PM

    Sorry, lighthouse messed up the layout. The basic steps would be:

    • compile the applet (javac Applet.java)
    • put the resulting file Applet.class into the java subdir of a webserver.
    • put the HTML snippet on any HTML page
    • open the HTML page.

    But as I said already: way out of scope for that ticket.

  • Juriy Zaytsev

    Juriy Zaytsev November 17th, 2008 @ 09:15 PM

    @eno

    Thanks. I'll see if I can get it to run.

    I'm just curious about such host objects' behavior. There were few reports before about similar anomalies (with either applet or object elements); browser would throw error during a simple property lookup. Knowing about such cases makes us design library more cautiously.

  • John-David Dalton
  • eno
  • Tobie Langel
  • Tobie Langel

    Tobie Langel July 23rd, 2009 @ 04:05 AM

    • Tag changed from perf to performance
  • T.J. Crowder

    T.J. Crowder November 16th, 2009 @ 04:50 PM

    • Assigned user cleared.

    [responsible:none bulk edit command]

  • Tobie Langel

    Tobie Langel March 1st, 2010 @ 01:25 AM

    • Tag changed from performance to performance, section:lang

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

Attachments

Referenced by

Pages