#968 enhancement
Michael M Slusarz

Inefficient String.unfilterJSON()

Reported by Michael M Slusarz | December 23rd, 2009 @ 06:12 AM

For the most common use case of unfilterJSON() - filtering out the comment delimiters as defined in Prototype.JSONFilter, the current version of unfilterJSON() is inefficient. There is no need to use a matching regex when the search strings are static, and their position is known (at beginning and end of string). This variation is much better:

/* More efficient String.unfilterJSON() function. */
Object.extend(String.prototype, {
    unfilterJSON: function(filter) {
        if (filter) {
            return this.replace(filter, '$1');
        } else if (this.startsWith('/*-secure-') &&
                   this.endsWith('*/')) {
            return this.slice(10, -2);
        return this;

(Granted, this doesn't handle leading/trailing whitespace, but that could easily be added with strip(). Or, even better, don't handle it and simply demand that the input string doesn't have this whitespace.)

Using FF 3.5.6, with a 900 KB string, the stock prototypejs version of unfilterJSON() fails with a script stack space exhaustion error. This modified code handles this data just fine.

Comments and changes to this ticket

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