From 37424d38d9fcb4e6d6b1f4d44816c5bc6e57da20 Mon Sep 17 00:00:00 2001 From: Jesse Tane Date: Sat, 22 Aug 2015 06:56:23 -0400 Subject: [PATCH] have a dedicated function for decoding code point arrays --- index.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) 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) { -- 2.34.1