RangeError: invalid array length

๋ฉ”์„ธ์ง€

RangeError: invalid array length (Firefox)
RangeError: Invalid array length (Chrome)
RangeError: Invalid array buffer length (Chrome)

์—๋Ÿฌ ํ˜•์‹

๋ฌด์—‡์ด ์ž˜๋ชป ๋œ ๊ฒƒ์ผ๊นŒ?

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์›์ธ ๋•Œ๋ฌธ์ด๋‹ค:

  • 232์™€ ๊ฐ™๊ฑฐ๋‚˜ ์ด๋ณด๋‹ค ๊ธด, ํ˜น์€ ์Œ์ˆ˜์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง„ Array๋‚˜ ArrayBuffer๋ฅผ ์ƒ์„ฑํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, ํ˜น์€
  • Array ์†์„ฑ์„ 232์™€ ๊ฐ™๊ฑฐ๋‚˜ ์ด๋ณด๋‹ค ๊ธด ๊ฐ’์œผ๋กœ ์„ค์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

Array์™€ ArrayBuffer์˜ ๊ธธ์ด์— ์ œํ•œ์„ ๋‘” ์ด์œ ๋Š”, Array๋‚˜ ArrayBuffer์˜ length ์†์„ฑ์€ ์‚ฌ์ธ๋˜์ง€ ์•Š์€(unsigned) 32 ๋น„ํŠธ ์ •์ˆ˜๋กœ ๋ฐ˜์˜๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ฆ‰ Array๋‚˜ ArrayBuffer๋Š” ์˜ค์ง 0 ~ 232-1 ์‚ฌ์ด์˜ ๊ฐ’๋งŒ์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

Array์˜ length๋กœ ํ•ด์„๋˜๋Š” ์ฒซ๋ฒˆ์งธ argument๋กœ์„œ ๋ฌธ์ž์—ด ํ‘œ๊ธฐ๋ฅผ ํ†ตํ•ด contructor๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Array๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š”, length ์†์„ฑ์„ ์„ค์ •ํ•˜๊ธฐ ์ „์— length์˜ ๊ธธ์ด์— ์ œํ•œ์„ ๋‘๊ฑฐ๋‚˜, constructor์˜ aurgment๋กœ์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ์ œ

์ž˜๋ชป๋œ ์˜ˆ์ œ

new Array(Math.pow(2, 40))
new Array(-1)
new ArrayBuffer(Math.pow(2, 32))
new ArrayBuffer(-1)

let a = [];
a.length = a.length - 1;         // set -1 to the length property

let b = new Array(Math.pow(2, 32) - 1);
b.length = b.length + 1;         // set 2^32 to the length property

์˜ฌ๋ฐ”๋ฅธ ์˜ˆ์ œ

[ Math.pow(2, 40) ]                     // [ 1099511627776 ]
[ -1 ]                                  // [ -1 ]
new ArrayBuffer(Math.pow(2, 32) - 1)
new ArrayBuffer(0)

let a = [];
a.length = Math.max(0, a.length - 1);

let b = new Array(Math.pow(2, 32) - 1);
b.length = Math.min(0xffffffff, b.length + 1);

// 0xffffffff is the hexadecimal notation for 2^32 - 1
// which can also be written as (-1 >>> 0)