Is there a limit on length of the key (string) in JS object?

It appears as though with ECMAScript 2016, there is now a definitive answer to this question. According to the MDN Web Docs on string.length:

ECMAScript 2016 (ed. 7) established a maximum length of 2^53 - 1 elements. Previously, no maximum length was specified.

You can also find this specified in the ECMAScript® 2016 Language Specification:

The String type is the set of all ordered sequences of zero or more 16-bit unsigned integer values (“elements”) up to a maximum length of 253-1 elements.


No, there is no limit for the string length (as long as it fits into memory), and your implementation seems okay too. It's acutally quite common to have those 'turned around' arrays with e.g. boolean values. And as to the strings as keys: The strings are immutable symbols that are stored at a certain address, and what's actually used as the index for the array is that address (aka pointer aka reference) and not the string itself.


I have researched this a bit.

MDN is silent on the issue, and so is the spec (ES5, ES6). They only state that the property accessor must be a string, without any qualifications – in other words, there is no limit as far as the spec is concerned. That's hardly surprising.

How browsers handle it, is another matter. I have set up a test and run it in a number of browsers. Chrome 40 (Desktop), Chrome 40 (Android 5.1), Firefox 36, Opera 27, and IE9+ can deal with a property name of up to 227 characters. Safari 8 (OS X Yosemite) can even handle property names of 230 characters.

For all those browsers except IE, the maximum property length is the same as the maximum string length. IE9+ can handle a maximum string length of ~230 characters, but the limit for object keys is at 227 characters, just as in the other browsers.

The test didn't work in IE8 and Safari on iOS, presumably due to memory issues caused by the test code.

In a nutshell, it is safe to use long property names, even when taking it to extremes. As long as the strings themselves stay within the limits of what browsers can handle, you can use them as property names as well.