Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Maximum length of an object property name">
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>Maximum length of an object property name - JS Bin</title>
</head>
<body>
  <h1>Maximum length of an object property name</h1>
  <p>The maximum length of a string in this browser is &lt; 2^<span class="maxedOut"></span> characters. Using a maximum string length of 2^<span class="strlim"></span>.</p>
  <p>Trying to create an object with a property name of this length has  <span class="maxLen creation"></span>. Trying to read the value back, using the property name, has <span class="maxLen retrieval"></span>.</p>
  <div class="reduced">
    <p>With a key of length 2^<span class="lim"></span>, object creation <span class="creation"></span> and value retrieval <span class="retrieval"></span>.</p>
  </div>
</body>
</html>
 
.failed {
  color: red;
}
.worked {
  color: green;
}
.reduced {
  display: none;
}
 
$( function () {
    var lim,
        success = false,
        result = "failed",
        obj = {},
        strLim = 0,
        base = "a";
    // Generating a string of length 2^strLim
    // until things blow up
    try {
        while ( true ) {
            strLim++;
            base += base;
        }
    } catch ( err ) {
        $( ".maxedOut" ).text( strLim );
        strLim--;
        lim = strLim;
        $( ".strlim" ).text( strLim );
    }
    while ( result === "failed" && lim > 0 ) {
        try {
            cleanup();
            // Creating an object with a key of
            // maximum length
            try {
                obj[base] = 1;
                result = "worked";
            } catch ( err ) {
                result = "failed";
            }
            showResult( ".creation", result, lim );
            // Reading the value back from the object,
            // using the key
            try {
                result = obj[base] === 1 ? "worked": "failed";
            } catch ( err ) {
                result = "failed";
            }
            showResult( ".retrieval", result, lim );
            
            if ( result === "failed" ) {
                lim--;
                base = getBaseString( 2^lim );
            }
        } catch ( e ) {
            result = "failed";
            while ( !success && lim > 0 ) {
                cleanup();
                try {
                    lim--;
                    base = getBaseString( 2^lim );
                    success = true;
                } catch ( err ) {}
            }
        }
    }
    // Helpers
    function showResult( selector, result, lim ) {
        if ( lim === strLim ) {
            $( ".maxLen" + selector )
                .addClass( result )
                .text( result );
        } else {
            $( ".reduced" ).show();
            $( ".reduced .lim" ).text( lim );
            $( ".reduced " + selector )
                .addClass( result )
                .text( result );
        }
    }
    // Creates a string of length 2^n
    function getBaseString( exponent ) {
        var i,
            base = "a";
        for ( i = 0; i < exponent; i++ ) base += base;
        return base;
    }
    // Tries to initiate garbage collection
    function cleanup() {
        if ( typeof CollectGarbage === "function" ) {
            // IE
            // See http://stackoverflow.com/a/15047781/508355
            CollectGarbage();
        } else if ( typeof gc === "function" ) {
            // Chrome with --js-flags="--expose-gc",
            // see http://stackoverflow.com/a/13951759/508355
            gc();
        } else if ( typeof opera !== "undefined" && typeof opera.collect === "function" ) {
            // Opera,
            // see http://stackoverflow.com/a/15047781/508355
            opera.collect();
        } else {
            try {
                // Gecko, but an old (2008) technique
                // See http://stackoverflow.com/a/10551271/508355
                window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                    .getInterface(Components.interfaces.nsIDOMWindowUtils)
                    .garbageCollect();
            } catch ( err ) {}
        }
    }
} );
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
hashchangepro
0viewers