Finding The Maximum Size of a Visualforce Remoting Parameter

Sometimes you just have to do something for kicks, but that doesn't mean the results can't prove useful. While trying to work out a way to solve a particular requirement for Proximity Insight, @bachovski and I wondered what the maximum length of a String parameter to a Visualforce remoting method would be.

To The Developer Tools!

The quickest way to test this was to try sending a large String to an existing method (that conveniently took a single String as a parameter) via the Firefox developer tools, and we did this like so:

PIRemoting.setPIN(Array(1024*1024*6).join('a'), function(res){});

This call promptly returned an error to the console to the effect that the parameter is too long. The String in this case would have been one byte less than 6 megabytes (the way this join() hack works means it's length is one less than the argument to Array()).

We chose 6 megabytes because that's the heap size in Apex, and seemed like a reasonable value to begin with (yes, parameters don't go on the heap but we had to start somewhere).

Getting Lazy

Through a few manual trials we got down to some lower numbers. It was greater than 0.9MB and less than 1.0MB, and at that point I decided it'd be faster to just hack some code together. The following does a shoddy (emphasis on the shoddy) job of running a binary search to find out what the value is.

For the record I don't recommend writing recursive code that has no termination condition. Ever. Even if you're being extremely lazy like me and just hacking something direct in the developer tools.

var min = 999000;  
var max = 999999;

var res = {};

function f()  
{
    console.log(' trying : ' + parseInt((min +  max) / 2));

    PIRemoting.setObj(Array(parseInt((min + max) / 2)).join("a"), function(r)
    {
        if(r && r.success)
        {
            min = (min + max) / 2;
        }
        else
        {
            max = (min + max) / 2;
        }

        f();
    });
}

The code above is crap, but it does work (for a given definition of 'work') and as you can see from the screen shot below we determined the maximum length to be 999774 characters. Yes the screenshot says 999775 but as explained above doing Array(5).join("a") actually generates the string "aaaa", and I was being so lazy that I didn't bother to subtract 1 in the output line.

Output from code you shouldn't write. It basically output  999775.
Doing things the dumb way, because why not?

So there you have it, if you need to know what the longest String length is that you can use with Visualforce remoting is, it's 999774 characters. Now I'd just to love to know why.

comments powered by Disqus