RangeError: repeat count must be less than infinity

๋ฉ”์‹œ์ง€

RangeError: repeat count must be less than infinity and not overflow maximum string size (Firefox)
(repeat count๋Š” infinity๋ณด๋‹ค ์ž‘์•„์•ผํ•˜๋ฉฐ, ์ตœ๋Œ€ ๋ฌธ์ž์—ด ํฌ๊ธฐ๋ฅผ ๋„˜๊ธธ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.)

RangeError: Invalid count value (Chrome)

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

๋ฌด์—‡์ด ์ž˜๋ชป๋˜์—ˆ์„๊นŒ?

 String.prototype.repeat() ๋ฉ”์†Œ๋“œ๊ฐ€ ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ˜๋ณตํ•˜๋Š” ์ˆ˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์นด์šดํŠธ ํŒŒ๋ผ๋ฉ”ํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ํŒŒ๋ผ๋ฉ”ํ„ฐ๋Š” 0๋ณด๋‹ค ์ปค์•ผํ•˜๋ฉฐ, ์–‘์˜ Infinity ๋ณด๋‹ค ์ž‘์•„์•ผ ํ–ˆ์œผ๋ฉฐ, ์Œ์ˆ˜๋Š” ํ—ˆ์šฉ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ’์˜ ํ—ˆ์šฉ ๋ฒ”์œ„๋Š” ์ด๋ ‡๊ฒŒ ํ‘œํ˜„๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. : [0, +โˆž). 

๊ฒฐ๊ณผ์ธ ๋ฌธ์ž์—ด์€ ์ตœ๋Œ€ ๋ฌธ์ž์—ด ํฌ๊ธฐ๋ณด๋‹ค ํด ์ˆ˜ ์—†์ง€๋งŒ, JavaScript ์—์„œ๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Firefox (SpiderMonkey) ์—์„œ์˜ ์ตœ๋Œ€ ๋ฌธ์ž์—ด ํฌ๊ธฐ๋Š” 228 -1 (0xFFFFFFF)์ž…๋‹ˆ๋‹ค.

์˜ˆ

ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

'abc'.repeat(Infinity); // RangeError
'a'.repeat(2**28);      // RangeError

ํ—ˆ์šฉ๋˜๋Š” ๊ฒฝ์šฐ

'abc'.repeat(0);    // ''
'abc'.repeat(1);    // 'abc'
'abc'.repeat(2);    // 'abcabc'
'abc'.repeat(3.5);  // 'abcabcabc' (์ˆซ์ž๋Š” ์ •์ˆ˜๋กœ ๋ณ€ํ™˜๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.)

์ฐธ์กฐ