From: Jesse Tane Date: Sat, 22 Aug 2015 10:56:23 +0000 (-0400) Subject: have a dedicated function for decoding code point arrays X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=37424d38d9fcb4e6d6b1f4d44816c5bc6e57da20;p=buffer.git have a dedicated function for decoding code point arrays --- diff --git a/index.js b/index.js index ba5eab0..06f7562 100644 --- a/index.js +++ b/index.js @@ -692,7 +692,30 @@ function utf8Slice (buf, start, end) { i += bytesPerSequence } - return binarySlice(res, 0, res.length) + return decodeCodePointsArray(res) +} + +// Based on http://stackoverflow.com/a/22747272/680742, the browser with +// the lowest limit is Chrome, with 0x10000 args. +// We go 1 magnitude less, for safety +var MAX_ARGUMENTS_LENGTH = 0x1000 + +function decodeCodePointsArray (codePoints) { + var len = codePoints.length + if (len <= MAX_ARGUMENTS_LENGTH) { + return String.fromCharCode.apply(String, codePoints) // avoid extra slice() + } + + // Decode in chunks to avoid "call stack size exceeded". + var res = '' + var i = 0 + while (i < len) { + res += String.fromCharCode.apply( + String, + codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH) + ) + } + return res } function asciiSlice (buf, start, end) {