Skip to content

Commit b460ca1

Browse files
committed
speedup ByteString and well-formed USVString
1 parent 7d0cfd3 commit b460ca1

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

lib/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,18 +252,23 @@ exports.DOMString = (value, options = {}) => {
252252

253253
exports.ByteString = (value, options = {}) => {
254254
const x = exports.DOMString(value, options);
255-
let c;
256-
for (let i = 0; (c = x.codePointAt(i)) !== undefined; ++i) {
257-
if (c > 255) {
258-
throw makeException(TypeError, "is not a valid ByteString", options);
259-
}
255+
256+
// Unicode regex is identical here but is ~5x slower on 16-bit strings
257+
// eslint-disable-next-line require-unicode-regexp
258+
if (!/[^\x00-\xff]/.test(x)) {
259+
return x;
260260
}
261261

262-
return x;
262+
throw makeException(TypeError, "is not a valid ByteString", options);
263263
};
264264

265265
exports.USVString = (value, options = {}) => {
266266
const S = exports.DOMString(value, options);
267+
268+
if (S.toWellFormed) {
269+
return S.toWellFormed();
270+
}
271+
267272
const n = S.length;
268273
const U = [];
269274
for (let i = 0; i < n; ++i) {

0 commit comments

Comments
 (0)