Skip to content

Commit 143a1e4

Browse files
committed
docs(ArrayBuffer): optimize ab2str()
1 parent 2a82f81 commit 143a1e4

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

docs/arraybuffer.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,17 @@ Float64Array.BYTES_PER_ELEMENT // 8
452452
```javascript
453453
// ArrayBuffer转为字符串,参数为ArrayBuffer对象
454454
function ab2str(buf) {
455-
return String.fromCharCode.apply(null, new Uint16Array(buf));
455+
if (buf && buf.byteLength < 128000) { // limit max length avoid overhead
456+
return String.fromCharCode.apply(null, new Uint16Array(buf));
457+
}
458+
459+
const len = bufView.byteLength;
460+
const bstr = new Array(len);
461+
const bufView = new Uint16Array(buf);
462+
for (let i = 0; i < len; i++) {
463+
bstr[i] = String.fromCharCode.call(null, bufView[i]);
464+
}
465+
return bstr.join('');
456466
}
457467

458468
// 字符串转为ArrayBuffer对象,参数为字符串

0 commit comments

Comments
 (0)