Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
fun with JS arrays
var a = [];
a[Math.pow(2, 32) - 2] = "max index"; // highest non-expando indexed property
console.log(a.length === Math.pow(2, 32) - 1); // true
try {
a.push("whoa", "EVEN MOAR WHOA");
} catch (e) {
console.log(e instanceof RangeError); // true
}
console.log(a.length === Math.pow(2, 32) - 1); // true
console.log(a[Math.pow(2, 32) - 1] === "whoa"); // true
console.log(a[Math.pow(2, 32)] === "EVEN MOAR WHOA"); // true
@floriancargoet

This comment has been minimized.

Copy link

@floriancargoet floriancargoet commented Nov 28, 2012

Another weird thing: invoking an unknown method on this "full" array crashes the tab in Chromium (25) instead of throwing a TypeError

var a = [];
a[Math.pow(2, 32) - 2] = "max index";
a.crashMyTab(); // "Aw, Snap! Something went wrong…" message
@erikcorry

This comment has been minimized.

Copy link

@erikcorry erikcorry commented Nov 28, 2012

It's an out of memory error. The attempt to format the exception string means you call 'toString' on the array, but the string representation is around 2 billion commas which isn't going to work. I filed http://code.google.com/p/v8/issues/detail?id=2431 for you

@erikcorry

This comment has been minimized.

Copy link

@erikcorry erikcorry commented Nov 28, 2012

As for the initial comment, see https://bugs.ecmascript.org/show_bug.cgi?id=131

@floriancargoet

This comment has been minimized.

Copy link

@floriancargoet floriancargoet commented Dec 7, 2012

@erikcorry Thank you for the bug report, I should have done that immediately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment