Skip to content

Commit 23b0296

Browse files
perf: faster encode_url and decode_url (#441)
* perf: faster `encode_url` and `decode_url` * feat: rm regexp --------- Signed-off-by: Mimi <[email protected]> Co-authored-by: Mimi <[email protected]>
1 parent ba23651 commit 23b0296

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/decode_url.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { parse, format } from 'url';
33
import { unescape } from 'querystring';
44

55
const decodeURL = (str: string) => {
6-
if (parse(str).protocol) {
6+
const index = str.indexOf(':');
7+
if (index < 0) {
8+
return unescape(str);
9+
}
10+
if (parse(str.slice(0, index + 1)).protocol) {
711
const parsed = new URL(str);
812

913
// Exit if input is a data url

lib/encode_url.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { parse, format } from 'url';
33
import { unescape } from 'querystring';
44

55
const encodeURL = (str: string) => {
6-
if (parse(str).protocol) {
6+
const index = str.indexOf(':');
7+
if (index < 0) {
8+
return encodeURI(unescape(str));
9+
}
10+
if (parse(str.slice(0, index + 1)).protocol) {
711
const parsed = new URL(str);
812

913
// Exit if input is a data url

0 commit comments

Comments
 (0)