Skip to content

Commit 658efad

Browse files
committed
hand-optimize
1 parent 5a874af commit 658efad

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

lib/loader/index.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,26 @@ const ARRAY_SIZE = 16;
3737
const BIGINT = typeof BigUint64Array !== "undefined";
3838
const THIS = Symbol();
3939

40-
const STRING_CHUNKSIZE = 1024;
40+
const STRING_SMALLSIZE = 192; // break-even point in V8
41+
const STRING_CHUNKSIZE = 1024; // mitigate stack overflow
42+
const utf16 = new TextDecoder("utf-16le"); // != wtf16
4143

4244
/** Gets a string from memory. */
4345
function getStringImpl(buffer, ptr) {
4446
let len = new Uint32Array(buffer)[ptr + SIZE_OFFSET >>> 2] >>> 1;
4547
const wtf16 = new Uint16Array(buffer, ptr, len);
46-
if (len <= STRING_CHUNKSIZE) return String.fromCharCode.apply(String, wtf16);
47-
let str = "";
48-
let off = 0;
49-
do {
50-
str += String.fromCharCode.apply(String, wtf16.subarray(off, off += STRING_CHUNKSIZE));
51-
len -= STRING_CHUNKSIZE;
52-
} while (len > STRING_CHUNKSIZE);
53-
return str + String.fromCharCode.apply(String, wtf16.subarray(off, off + len));
48+
if (len <= STRING_SMALLSIZE) return String.fromCharCode.apply(String, wtf16);
49+
try {
50+
return utf16.decode(wtf16, { fatal: true });
51+
} catch {
52+
let str = "";
53+
let off = 0;
54+
while (len > STRING_CHUNKSIZE) {
55+
str += String.fromCharCode.apply(String, wtf16.subarray(off, off += STRING_CHUNKSIZE));
56+
len -= STRING_CHUNKSIZE;
57+
}
58+
return str + String.fromCharCode.apply(String, wtf16.subarray(off, off + len));
59+
}
5460
}
5561

5662
/** Prepares the base module prior to instantiation. */

lib/loader/umd/index.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,34 @@ var loader = (function(exports) {
4444
const ARRAY_SIZE = 16;
4545
const BIGINT = typeof BigUint64Array !== "undefined";
4646
const THIS = Symbol();
47-
const STRING_CHUNKSIZE = 1024;
47+
const STRING_SMALLSIZE = 192; // break-even point in V8
48+
49+
const STRING_CHUNKSIZE = 1024; // mitigate stack overflow
50+
51+
const utf16 = new TextDecoder("utf-16le"); // != wtf16
52+
4853
/** Gets a string from memory. */
4954

5055
function getStringImpl(buffer, ptr) {
5156
let len = new Uint32Array(buffer)[ptr + SIZE_OFFSET >>> 2] >>> 1;
5257
const wtf16 = new Uint16Array(buffer, ptr, len);
53-
if (len <= STRING_CHUNKSIZE) return String.fromCharCode.apply(String, wtf16);
54-
let str = "";
55-
let off = 0;
58+
if (len <= STRING_SMALLSIZE) return String.fromCharCode.apply(String, wtf16);
5659

57-
do {
58-
str += String.fromCharCode.apply(String, wtf16.subarray(off, off += STRING_CHUNKSIZE));
59-
len -= STRING_CHUNKSIZE;
60-
} while (len > STRING_CHUNKSIZE);
60+
try {
61+
return utf16.decode(wtf16, {
62+
fatal: true
63+
});
64+
} catch {
65+
let str = "";
66+
let off = 0;
6167

62-
return str + String.fromCharCode.apply(String, wtf16.subarray(off, off + len));
68+
while (len > STRING_CHUNKSIZE) {
69+
str += String.fromCharCode.apply(String, wtf16.subarray(off, off += STRING_CHUNKSIZE));
70+
len -= STRING_CHUNKSIZE;
71+
}
72+
73+
return str + String.fromCharCode.apply(String, wtf16.subarray(off, off + len));
74+
}
6375
}
6476
/** Prepares the base module prior to instantiation. */
6577

0 commit comments

Comments
 (0)