* Transform an array-like object to a string. * @param {Array|ArrayBuffer|Uint8Array|Buffer} array the array to transform. * @return {String} the result.
(array)
| 162 | * @return {String} the result. |
| 163 | */ |
| 164 | function arrayLikeToString(array) { |
| 165 | // Performances notes : |
| 166 | // -------------------- |
| 167 | // String.fromCharCode.apply(null, array) is the fastest, see |
| 168 | // see http://jsperf.com/converting-a-uint8array-to-a-string/2 |
| 169 | // but the stack is limited (and we can get huge arrays !). |
| 170 | // |
| 171 | // result += String.fromCharCode(array[i]); generate too many strings ! |
| 172 | // |
| 173 | // This code is inspired by http://jsperf.com/arraybuffer-to-string-apply-performance/2 |
| 174 | // TODO : we now have workers that split the work. Do we still need that ? |
| 175 | var chunk = 65536, |
| 176 | type = exports.getTypeOf(array), |
| 177 | canUseApply = true; |
| 178 | if (type === "uint8array") { |
| 179 | canUseApply = arrayToStringHelper.applyCanBeUsed.uint8array; |
| 180 | } else if (type === "nodebuffer") { |
| 181 | canUseApply = arrayToStringHelper.applyCanBeUsed.nodebuffer; |
| 182 | } |
| 183 | |
| 184 | if (canUseApply) { |
| 185 | while (chunk > 1) { |
| 186 | try { |
| 187 | return arrayToStringHelper.stringifyByChunk(array, type, chunk); |
| 188 | } catch (e) { |
| 189 | chunk = Math.floor(chunk / 2); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // no apply or chunk error : slow and painful algorithm |
| 195 | // default browser on android 4.* |
| 196 | return arrayToStringHelper.stringifyByChar(array); |
| 197 | } |
| 198 | |
| 199 | exports.applyFromCharCode = arrayLikeToString; |
| 200 |