<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 < 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
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |