fast-querystring
Advanced tools
Comparing version 1.1.2 to 1.1.0
@@ -0,8 +1,26 @@ | ||
"use strict"; | ||
// This file is taken from Node.js project. | ||
// Full implementation can be found from https://github.com/nodejs/node/blob/main/lib/internal/querystring.js | ||
const hexTable = Array.from( | ||
{ length: 256 }, | ||
(_, i) => "%" + ((i < 16 ? "0" : "") + i.toString(16)).toUpperCase(), | ||
); | ||
// rome-ignore format: the array should not be formatted | ||
const hexTable = [ | ||
"%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07", "%08", "%09", "%0A", "%0B", "%0C", "%0D", "%0E", "%0F", | ||
"%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17", "%18", "%19", "%1A", "%1B", "%1C", "%1D", "%1E", "%1F", | ||
"%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27", "%28", "%29", "%2A", "%2B", "%2C", "%2D", "%2E", "%2F", | ||
"%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37", "%38", "%39", "%3A", "%3B", "%3C", "%3D", "%3E", "%3F", | ||
"%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47", "%48", "%49", "%4A", "%4B", "%4C", "%4D", "%4E", "%4F", | ||
"%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57", "%58", "%59", "%5A", "%5B", "%5C", "%5D", "%5E", "%5F", | ||
"%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67", "%68", "%69", "%6A", "%6B", "%6C", "%6D", "%6E", "%6F", | ||
"%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77", "%78", "%79", "%7A", "%7B", "%7C", "%7D", "%7E", "%7F", | ||
"%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87", "%88", "%89", "%8A", "%8B", "%8C", "%8D", "%8E", "%8F", | ||
"%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97", "%98", "%99", "%9A", "%9B", "%9C", "%9D", "%9E", "%9F", | ||
"%A0", "%A1", "%A2", "%A3", "%A4", "%A5", "%A6", "%A7", "%A8", "%A9", "%AA", "%AB", "%AC", "%AD", "%AE", "%AF", | ||
"%B0", "%B1", "%B2", "%B3", "%B4", "%B5", "%B6", "%B7", "%B8", "%B9", "%BA", "%BB", "%BC", "%BD", "%BE", "%BF", | ||
"%C0", "%C1", "%C2", "%C3", "%C4", "%C5", "%C6", "%C7", "%C8", "%C9", "%CA", "%CB", "%CC", "%CD", "%CE", "%CF", | ||
"%D0", "%D1", "%D2", "%D3", "%D4", "%D5", "%D6", "%D7", "%D8", "%D9", "%DA", "%DB", "%DC", "%DD", "%DE", "%DF", | ||
"%E0", "%E1", "%E2", "%E3", "%E4", "%E5", "%E6", "%E7", "%E8", "%E9", "%EA", "%EB", "%EC", "%ED", "%EE", "%EF", | ||
"%F0", "%F1", "%F2", "%F3", "%F4", "%F5", "%F6", "%F7", "%F8", "%F9", "%FA", "%FB", "%FC", "%FD", "%FE", "%FF" | ||
]; | ||
const utf16 = require("./utf16"); | ||
@@ -33,3 +51,3 @@ // These characters do not need escaping when generating query strings: | ||
const len = str.length; | ||
if (len === 0) return ""; | ||
if (len === 0) return str; | ||
@@ -39,54 +57,54 @@ let out = ""; | ||
let i = 0; | ||
let c = 0; | ||
outer: for (; i < len; i++) { | ||
let c = str.charCodeAt(i); | ||
while (i < len) { | ||
c = str.charCodeAt(i); | ||
// ASCII | ||
while (c < 0x80) { | ||
if (noEscape[c] !== 1) { | ||
if (c < 0x80) { | ||
if (noEscape[c] === 1) { | ||
++i; | ||
} else { | ||
if (lastPos < i) out += str.slice(lastPos, i); | ||
lastPos = i + 1; | ||
lastPos = ++i; | ||
out += hexTable[c]; | ||
} | ||
// Multi-byte characters ... | ||
} else { | ||
if (lastPos < i) { | ||
out += str.slice(lastPos, i); | ||
} | ||
if (++i === len) break outer; | ||
if (c < 0x800) { | ||
lastPos = ++i; | ||
out += utf16[c]; | ||
} else if (c < 0xd800) { | ||
lastPos = ++i; | ||
out += utf16[c]; | ||
} else if (c < 0xe000) { | ||
// Surrogate pair | ||
c = str.charCodeAt(i); | ||
} | ||
// This branch should never happen because all URLSearchParams entries | ||
// should already be converted to USVString. But, included for | ||
// completion's sake anyway. | ||
if (++i === len) { | ||
throw new Error("URI malformed"); | ||
} | ||
if (lastPos < i) out += str.slice(lastPos, i); | ||
// Multi-byte characters ... | ||
if (c < 0x800) { | ||
lastPos = i + 1; | ||
out += hexTable[0xc0 | (c >> 6)] + hexTable[0x80 | (c & 0x3f)]; | ||
continue; | ||
c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff)); | ||
lastPos = ++i; | ||
out += | ||
hexTable[0xf0 | (c >> 18)] + | ||
hexTable[0x80 | ((c >> 12) & 0x3f)] + | ||
hexTable[0x80 | ((c >> 6) & 0x3f)] + | ||
hexTable[0x80 | (c & 0x3f)]; | ||
} else { | ||
if (lastPos < i) { | ||
out += str.slice(lastPos, i); | ||
} | ||
// Multi-byte characters ... | ||
lastPos = ++i; | ||
out += utf16[c]; | ||
} | ||
} | ||
if (c < 0xd800 || c >= 0xe000) { | ||
lastPos = i + 1; | ||
out += | ||
hexTable[0xe0 | (c >> 12)] + | ||
hexTable[0x80 | ((c >> 6) & 0x3f)] + | ||
hexTable[0x80 | (c & 0x3f)]; | ||
continue; | ||
} | ||
// Surrogate pair | ||
++i; | ||
// This branch should never happen because all URLSearchParams entries | ||
// should already be converted to USVString. But, included for | ||
// completion's sake anyway. | ||
if (i >= len) { | ||
throw new Error("URI malformed"); | ||
} | ||
const c2 = str.charCodeAt(i) & 0x3ff; | ||
lastPos = i + 1; | ||
c = 0x10000 + (((c & 0x3ff) << 10) | c2); | ||
out += | ||
hexTable[0xf0 | (c >> 18)] + | ||
hexTable[0x80 | ((c >> 12) & 0x3f)] + | ||
hexTable[0x80 | ((c >> 6) & 0x3f)] + | ||
hexTable[0x80 | (c & 0x3f)]; | ||
} | ||
@@ -93,0 +111,0 @@ if (lastPos === 0) return str; |
{ | ||
"name": "fast-querystring", | ||
"version": "1.1.2", | ||
"version": "1.1.0", | ||
"description": "A fast alternative to legacy querystring module", | ||
@@ -11,4 +11,2 @@ "main": "./lib/index.js", | ||
"test": "vitest", | ||
"test:environment:edge": "vitest --environment=edge-runtime", | ||
"test:environment:browser": "vitest --environment=jsdom", | ||
"test:watch": "vitest --watch", | ||
@@ -31,20 +29,17 @@ "test:coverage": "vitest --coverage", | ||
"devDependencies": { | ||
"@aws-sdk/querystring-builder": "^3.342.0", | ||
"@aws-sdk/querystring-parser": "^3.342.0", | ||
"@edge-runtime/vm": "^3.0.1", | ||
"@types/node": "^20.2.5", | ||
"@vitest/coverage-c8": "^0.31.4", | ||
"@aws-sdk/querystring-builder": "^3.201.0", | ||
"@aws-sdk/querystring-parser": "^3.201.0", | ||
"@types/node": "^18.11.9", | ||
"@vitest/coverage-c8": "^0.26.3", | ||
"benchmark": "^2.1.4", | ||
"cli-select": "^1.1.2", | ||
"cronometro": "^1.1.5", | ||
"cronometro": "^1.1.4", | ||
"http-querystring-stringify": "^2.1.0", | ||
"jsdom": "^22.1.0", | ||
"qs": "^6.11.2", | ||
"qs": "^6.11.0", | ||
"query-string": "^8.1.0", | ||
"querystringify": "^2.2.0", | ||
"querystringify-ts": "^0.1.5", | ||
"querystringparser": "^0.1.1", | ||
"rome": "12.1.3", | ||
"simple-git": "^3.19.0", | ||
"vitest": "^0.31.4" | ||
"rome": "11.0.0", | ||
"simple-git": "^3.14.1", | ||
"vitest": "^0.26.3" | ||
}, | ||
@@ -51,0 +46,0 @@ "repository": { |
@@ -47,4 +47,2 @@ # fast-querystring | ||
All benchmarks are run using Node.js v20.2.0 running on M1 Max. | ||
- Parsing a query-string | ||
@@ -58,14 +56,14 @@ | ||
╟─────────────────────────────────────────┼─────────┼───────────────────┼───────────╢ | ||
║ query-string │ 10000 │ 273968.62 op/sec │ ± 1.48 % ║ | ||
║ qs │ 9999 │ 324118.68 op/sec │ ± 0.99 % ║ | ||
║ querystringify │ 1000 │ 410157.64 op/sec │ ± 0.68 % ║ | ||
║ @aws-sdk/querystring-parser │ 1000 │ 431465.20 op/sec │ ± 0.83 % ║ | ||
║ URLSearchParams-with-Object.fromEntries │ 5000 │ 833939.19 op/sec │ ± 0.97 % ║ | ||
║ URLSearchParams-with-construct │ 10000 │ 980017.92 op/sec │ ± 2.42 % ║ | ||
║ node:querystring │ 10000 │ 1068165.86 op/sec │ ± 3.41 % ║ | ||
║ querystringparser │ 3000 │ 1384001.31 op/sec │ ± 0.95 % ║ | ||
║ qs │ 10000 │ 350884.75 op/sec │ ± 1.36 % ║ | ||
║ query-string │ 10000 │ 383165.31 op/sec │ ± 1.22 % ║ | ||
║ querystringify │ 1500 │ 530280.43 op/sec │ ± 0.90 % ║ | ||
║ @aws-sdk/querystring-parser │ 2000 │ 556657.27 op/sec │ ± 0.79 % ║ | ||
║ URLSearchParams-with-Object.fromEntries │ 10000 │ 845766.67 op/sec │ ± 2.85 % ║ | ||
║ URLSearchParams-with-construct │ 10000 │ 1158368.83 op/sec │ ± 4.28 % ║ | ||
║ node:querystring │ 2000 │ 1460476.58 op/sec │ ± 0.96 % ║ | ||
║ querystringparser │ 10000 │ 1976384.97 op/sec │ ± 4.11 % ║ | ||
╟─────────────────────────────────────────┼─────────┼───────────────────┼───────────╢ | ||
║ Fastest test │ Samples │ Result │ Tolerance ║ | ||
╟─────────────────────────────────────────┼─────────┼───────────────────┼───────────╢ | ||
║ fast-querystring │ 10000 │ 1584458.62 op/sec │ ± 2.64 % ║ | ||
║ fast-querystring │ 10000 │ 2123713.08 op/sec │ ± 2.87 % ║ | ||
╚═════════════════════════════════════════╧═════════╧═══════════════════╧═══════════╝ | ||
@@ -82,36 +80,15 @@ ``` | ||
╟──────────────────────────────┼─────────┼───────────────────┼───────────╢ | ||
║ query-string │ 10000 │ 314662.25 op/sec │ ± 1.08 % ║ | ||
║ qs │ 9500 │ 353621.74 op/sec │ ± 0.98 % ║ | ||
║ http-querystring-stringify │ 10000 │ 372189.04 op/sec │ ± 1.48 % ║ | ||
║ @aws-sdk/querystring-builder │ 10000 │ 411658.63 op/sec │ ± 1.67 % ║ | ||
║ URLSearchParams │ 10000 │ 454438.85 op/sec │ ± 1.32 % ║ | ||
║ querystringparser │ 10000 │ 455615.18 op/sec │ ± 4.22 % ║ | ||
║ querystringify │ 10000 │ 879020.96 op/sec │ ± 2.12 % ║ | ||
║ querystringify-ts │ 10000 │ 879134.48 op/sec │ ± 2.19 % ║ | ||
║ node:querystring │ 10000 │ 1244505.97 op/sec │ ± 2.12 % ║ | ||
║ query-string │ 10000 │ 310383.60 op/sec │ ± 1.14 % ║ | ||
║ qs │ 10000 │ 354332.59 op/sec │ ± 1.23 % ║ | ||
║ @aws-sdk/querystring-builder │ 10000 │ 411500.38 op/sec │ ± 1.30 % ║ | ||
║ http-querystring-stringify │ 1500 │ 535883.47 op/sec │ ± 1.00 % ║ | ||
║ URLSearchParams │ 10000 │ 594068.52 op/sec │ ± 1.61 % ║ | ||
║ querystringparser │ 10000 │ 766081.64 op/sec │ ± 2.18 % ║ | ||
║ querystringify │ 10000 │ 914083.38 op/sec │ ± 1.67 % ║ | ||
║ node:querystring │ 4000 │ 1822536.85 op/sec │ ± 0.91 % ║ | ||
╟──────────────────────────────┼─────────┼───────────────────┼───────────╢ | ||
║ Fastest test │ Samples │ Result │ Tolerance ║ | ||
╟──────────────────────────────┼─────────┼───────────────────┼───────────╢ | ||
║ fast-querystring │ 10000 │ 1953717.60 op/sec │ ± 3.16 % ║ | ||
║ fast-querystring │ 10000 │ 2186435.62 op/sec │ ± 3.48 % ║ | ||
╚══════════════════════════════╧═════════╧═══════════════════╧═══════════╝ | ||
``` | ||
- Importing package. | ||
``` | ||
> node benchmark/import.mjs | ||
╔═════════════════════════════╤═════════╤═════════════════╤═══════════╗ | ||
║ Slower tests │ Samples │ Result │ Tolerance ║ | ||
╟─────────────────────────────┼─────────┼─────────────────┼───────────╢ | ||
║ @aws-sdk/querystring-parser │ 1000 │ 12360.51 op/sec │ ± 0.57 % ║ | ||
║ qs │ 1000 │ 14507.74 op/sec │ ± 0.36 % ║ | ||
║ querystringify │ 1000 │ 14750.53 op/sec │ ± 0.39 % ║ | ||
║ query-string │ 1000 │ 16335.05 op/sec │ ± 0.87 % ║ | ||
║ querystringparser │ 1000 │ 17018.50 op/sec │ ± 0.42 % ║ | ||
╟─────────────────────────────┼─────────┼─────────────────┼───────────╢ | ||
║ Fastest test │ Samples │ Result │ Tolerance ║ | ||
╟─────────────────────────────┼─────────┼─────────────────┼───────────╢ | ||
║ fast-querystring │ 2500 │ 74605.83 op/sec │ ± 0.91 % ║ | ||
╚═════════════════════════════╧═════════╧═════════════════╧═══════════╝ | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
780318
15
9
3273
92
1