GeezerVison is showing github.com — querystring: improve stringify() performance · windwiny/node@7b46793
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: benchmark/querystring/querystring-stringify.js +7-2Lines changed: 7 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,7 +3,7 @@ const common = require('../common.js'); | 3 | 3 | const querystring = require('querystring'); | 4 | 4 |
| 5 | 5 | const bench = common.createBenchmark(main, { | 6 | | - type: ['noencode', 'encodemany', 'encodelast', 'array'], | | 6 | + type: ['noencode', 'encodemany', 'encodelast', 'array', 'multiprimitives'], | 7 | 7 | n: [1e6], | 8 | 8 | }); | 9 | 9 |
| @@ -28,7 +28,12 @@ function main({ type, n }) { | 28 | 28 | foo: [], | 29 | 29 | baz: ['bar'], | 30 | 30 | xyzzy: ['bar', 'quux', 'thud'] | 31 | | - } | | 31 | + }, | | 32 | + multiprimitives: { | | 33 | + foo: false, | | 34 | + bar: -13.37, | | 35 | + baz: '', | | 36 | + }, | 32 | 37 | }; | 33 | 38 | const input = inputs[type]; | 34 | 39 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/querystring.js +15-9Lines changed: 15 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,19 +36,25 @@ function encodeStr(str, noEscapeTable, hexTable) { | 36 | 36 |
| 37 | 37 | let out = ''; | 38 | 38 | let lastPos = 0; | | 39 | + let i = 0; | 39 | 40 |
| 40 | | - for (let i = 0; i < len; i++) { | | 41 | + outer: | | 42 | + for (; i < len; i++) { | 41 | 43 | let c = str.charCodeAt(i); | 42 | 44 |
| 43 | 45 | // ASCII | 44 | | - if (c < 0x80) { | 45 | | - if (noEscapeTable[c] === 1) | 46 | | - continue; | 47 | | - if (lastPos < i) | 48 | | - out += str.slice(lastPos, i); | 49 | | - lastPos = i + 1; | 50 | | - out += hexTable[c]; | 51 | | - continue; | | 46 | + while (c < 0x80) { | | 47 | + if (noEscapeTable[c] !== 1) { | | 48 | + if (lastPos < i) | | 49 | + out += str.slice(lastPos, i); | | 50 | + lastPos = i + 1; | | 51 | + out += hexTable[c]; | | 52 | + } | | 53 | + | | 54 | + if (++i === len) | | 55 | + break outer; | | 56 | + | | 57 | + c = str.charCodeAt(i); | 52 | 58 | } | 53 | 59 |
| 54 | 60 | if (lastPos < i) | |
Collapse file Copy file name to clipboardExpand all lines: lib/querystring.js +25-3Lines changed: 25 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 26 | 26 | const { | 27 | 27 | Array, | 28 | 28 | ArrayIsArray, | | 29 | + MathAbs, | 29 | 30 | ObjectCreate, | 30 | 31 | ObjectKeys, | 31 | 32 | } = primordials; | @@ -162,6 +163,25 @@ function stringifyPrimitive(v) { | 162 | 163 | } | 163 | 164 |
| 164 | 165 |
| | 166 | +function encodeStringified(v, encode) { | | 167 | + if (typeof v === 'string') | | 168 | + return (v.length ? encode(v) : ''); | | 169 | + if (typeof v === 'number' && isFinite(v)) { | | 170 | + // Values >= 1e21 automatically switch to scientific notation which requires | | 171 | + // escaping due to the inclusion of a '+' in the output | | 172 | + return (MathAbs(v) < 1e21 ? '' + v : encode('' + v)); | | 173 | + } | | 174 | + if (typeof v === 'boolean') | | 175 | + return v ? 'true' : 'false'; | | 176 | + return ''; | | 177 | +} | | 178 | + | | 179 | + | | 180 | +function encodeStringifiedCustom(v, encode) { | | 181 | + return encode(stringifyPrimitive(v)); | | 182 | +} | | 183 | + | | 184 | + | 165 | 185 | function stringify(obj, sep, eq, options) { | 166 | 186 | sep = sep || '&'; | 167 | 187 | eq = eq || '='; | @@ -170,6 +190,8 @@ function stringify(obj, sep, eq, options) { | 170 | 190 | if (options && typeof options.encodeURIComponent === 'function') { | 171 | 191 | encode = options.encodeURIComponent; | 172 | 192 | } | | 193 | + const convert = | | 194 | + (encode === qsEscape ? encodeStringified : encodeStringifiedCustom); | 173 | 195 |
| 174 | 196 | if (obj !== null && typeof obj === 'object') { | 175 | 197 | const keys = ObjectKeys(obj); | @@ -179,7 +201,7 @@ function stringify(obj, sep, eq, options) { | 179 | 201 | for (let i = 0; i < len; ++i) { | 180 | 202 | const k = keys[i]; | 181 | 203 | const v = obj[k]; | 182 | | - let ks = encode(stringifyPrimitive(k)); | | 204 | + let ks = convert(k, encode); | 183 | 205 | ks += eq; | 184 | 206 |
| 185 | 207 | if (ArrayIsArray(v)) { | @@ -188,13 +210,13 @@ function stringify(obj, sep, eq, options) { | 188 | 210 | const vlast = vlen - 1; | 189 | 211 | for (let j = 0; j < vlen; ++j) { | 190 | 212 | fields += ks; | 191 | | - fields += encode(stringifyPrimitive(v[j])); | | 213 | + fields += convert(v[j], encode); | 192 | 214 | if (j < vlast) | 193 | 215 | fields += sep; | 194 | 216 | } | 195 | 217 | } else { | 196 | 218 | fields += ks; | 197 | | - fields += encode(stringifyPrimitive(v)); | | 219 | + fields += convert(v, encode); | 198 | 220 | } | 199 | 221 |
| 200 | 222 | if (i < flast) | |
|