a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky
atproto bluesky typescript npm
101
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(uint8array): attempt fast-path ascii-only decoding for small strings

Mary ede65cf9 44deaf28

+40 -2
+5
.changeset/five-readers-hear.md
··· 1 + --- 2 + '@atcute/uint8array': patch 3 + --- 4 + 5 + attempt fast-path ASCII-only decoding for small strings
+35 -2
packages/utilities/uint8array/lib/index.ts
··· 128 128 return result.written; 129 129 }; 130 130 131 + const fromCharCode = String.fromCharCode; 132 + 131 133 /** 132 134 * decodes a UTF-8 string from a buffer 133 135 */ ··· 142 144 buffer = from.subarray(offset, offset + length); 143 145 } 144 146 145 - const result = textDecoder.decode(buffer); 147 + const end = buffer.length; 148 + if (end > 24) { 149 + return textDecoder.decode(buffer); 150 + } 151 + 152 + { 153 + let str = ''; 154 + let idx = 0; 155 + 156 + for (; idx + 3 < end; idx += 4) { 157 + const a = buffer[idx]; 158 + const b = buffer[idx + 1]; 159 + const c = buffer[idx + 2]; 160 + const d = buffer[idx + 3]; 161 + 162 + if ((a | b | c | d) & 0x80) { 163 + return str + textDecoder.decode(buffer.subarray(idx)); 164 + } 165 + 166 + str += fromCharCode(a, b, c, d); 167 + } 168 + 169 + for (; idx < end; idx++) { 170 + const x = buffer[idx]; 146 171 147 - return result; 172 + if (x & 0x80) { 173 + return str + textDecoder.decode(buffer.subarray(idx)); 174 + } 175 + 176 + str += fromCharCode(x); 177 + } 178 + 179 + return str; 180 + } 148 181 }; 149 182 150 183 /**