Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve parse performance by 12% #15

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@ console.log(qs.stringify({ foo: ['bar', 'baz'] }))
╔═════════════════════════════════════════╤═════════╤═══════════════════╤═══════════╗
║ Slower tests │ Samples │ Result │ Tolerance ║
╟─────────────────────────────────────────┼─────────┼───────────────────┼───────────╢
║ qs │ 10000 │ 317841.53 op/sec │ ± 1.18 % ║
║ query-string │ 10000 │ 341009.91 op/sec │ ± 1.06 % ║
║ querystringify │ 1000 │ 451609.79 op/sec │ ± 0.61 % ║
║ @aws-sdk/querystring-parser │ 1000 │ 477241.09 op/sec │ ± 0.69 % ║
║ URLSearchParams-with-Object.fromEntries │ 10000 │ 870095.07 op/sec │ ± 3.29 % ║
║ URLSearchParams-with-construct │ 10000 │ 1232650.60 op/sec │ ± 2.95 % ║
║ node:querystring │ 10000 │ 1462802.04 op/sec │ ± 3.69 % ║
║ qs │ 10000 │ 302595.31 op/sec │ ± 1.22 % ║
║ query-string │ 9500 │ 334820.82 op/sec │ ± 0.99 % ║
║ querystringify │ 1000 │ 437899.50 op/sec │ ± 0.73 % ║
║ @aws-sdk/querystring-parser │ 1000 │ 454836.96 op/sec │ ± 0.69 % ║
║ URLSearchParams-with-Object.fromEntries │ 1500 │ 849572.92 op/sec │ ± 0.89 % ║
║ URLSearchParams-with-construct │ 10000 │ 1190835.28 op/sec │ ± 3.22 % ║
║ node:querystring │ 10000 │ 1384717.43 op/sec │ ± 2.99 % ║
║ querystringparser │ 3500 │ 1735544.65 op/sec │ ± 0.95 % ║
╟─────────────────────────────────────────┼─────────┼───────────────────┼───────────╢
║ Fastest test │ Samples │ Result │ Tolerance ║
╟─────────────────────────────────────────┼─────────┼───────────────────┼───────────╢
║ fast-querystring │ 10000 │ 1809816.84 op/sec │ ± 3.19 % ║
║ fast-querystring │ 10000 │ 2023187.35 op/sec │ ± 2.67 % ║
╚═════════════════════════════════════════╧═════════╧═══════════════════╧═══════════╝
```

Expand All @@ -67,18 +68,20 @@ console.log(qs.stringify({ foo: ['bar', 'baz'] }))
```
> node benchmark/stringify.mjs

╔════════════════════════════╤═════════╤═══════════════════╤═══════════╗
║ Slower tests │ Samples │ Result │ Tolerance ║
╟────────────────────────────┼─────────┼───────────────────┼───────────╢
║ query-string │ 10000 │ 284130.63 op/sec │ ± 1.62 % ║
║ qs │ 10000 │ 334799.48 op/sec │ ± 1.93 % ║
║ http-querystring-stringify │ 10000 │ 482642.49 op/sec │ ± 1.72 % ║
║ URLSearchParams │ 10000 │ 587274.65 op/sec │ ± 1.88 % ║
║ querystringify │ 10000 │ 753960.35 op/sec │ ± 2.20 % ║
║ node:querystring │ 10000 │ 1796993.95 op/sec │ ± 5.34 % ║
╟────────────────────────────┼─────────┼───────────────────┼───────────╢
║ Fastest test │ Samples │ Result │ Tolerance ║
╟────────────────────────────┼─────────┼───────────────────┼───────────╢
║ fast-querystring │ 10000 │ 2051022.89 op/sec │ ± 4.52 % ║
╚════════════════════════════╧═════════╧═══════════════════╧═══════════╝
╔══════════════════════════════╤═════════╤═══════════════════╤═══════════╗
║ Slower tests │ Samples │ Result │ Tolerance ║
╟──────────────────────────────┼─────────┼───────────────────┼───────────╢
║ query-string │ 10000 │ 294354.42 op/sec │ ± 1.25 % ║
║ qs │ 10000 │ 349992.31 op/sec │ ± 1.45 % ║
║ @aws-sdk/querystring-builder │ 10000 │ 380426.03 op/sec │ ± 1.69 % ║
║ http-querystring-stringify │ 10000 │ 489248.93 op/sec │ ± 1.54 % ║
║ URLSearchParams │ 10000 │ 579241.21 op/sec │ ± 1.92 % ║
║ querystringparser │ 1500 │ 667303.72 op/sec │ ± 0.77 % ║
║ querystringify │ 10000 │ 780283.61 op/sec │ ± 2.43 % ║
║ node:querystring │ 10000 │ 1779241.34 op/sec │ ± 6.49 % ║
╟──────────────────────────────┼─────────┼───────────────────┼───────────╢
║ Fastest test │ Samples │ Result │ Tolerance ║
╟──────────────────────────────┼─────────┼───────────────────┼───────────╢
║ fast-querystring │ 10000 │ 2125769.45 op/sec │ ± 3.93 % ║
╚══════════════════════════════╧═════════╧═══════════════════╧═══════════╝
```
4 changes: 4 additions & 0 deletions benchmark/parse.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import native from "node:querystring";
import queryString from "query-string";
import querystringify from "querystringify";
import awsQueryStringParser from "@aws-sdk/querystring-parser";
import querystringparser from "querystringparser";

const input = "frappucino=muffin&goat=scone&pond=moose&foo=bar&foo=baz";

Expand Down Expand Up @@ -46,6 +47,9 @@ await benchmark(
"@aws-sdk/querystring-parser"() {
return awsQueryStringParser.parseQueryString(input);
},
querystringparser() {
return querystringparser.parse(input);
},
},
{ warmup: true },
);
8 changes: 8 additions & 0 deletions benchmark/stringify.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import native from "node:querystring";
import queryString from "query-string";
import querystringify from "querystringify";
import httpQuerystringStringify from "http-querystring-stringify";
import awsQueryStringBuilder from "@aws-sdk/querystring-builder";
import querystringparser from "querystringparser";

const value = {
frappucino: "muffin",
Expand Down Expand Up @@ -40,6 +42,12 @@ await benchmark(
"http-querystring-stringify"() {
return httpQuerystringStringify(value);
},
"@aws-sdk/querystring-builder"() {
return awsQueryStringBuilder.buildQueryString(value);
},
querystringparser() {
return querystringparser.stringify(value);
},
},
{ warmup: true },
);
44 changes: 21 additions & 23 deletions lib/parse.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
"use strict";

const fastDecode = require("fast-decode-uri-component");

const plusRegex = /\+/g;
const Empty = function () {};
Empty.prototype = Object.create(null);

/**
* @callback parse
* @param {string} input
*/
function parse(input) {
const result = Object.create(null);
// Optimization: Use new Empty() instead of Object.create(null) for performance
// v8 has a better optimization for initializing functions compared to Object
const result = new Empty();

if (typeof input !== "string") {
return result;
}

const inputLength = input.length;
let inputLength = input.length;
let key = "";
let value = "";
let startingIndex = -1;
Expand All @@ -37,13 +39,8 @@ function parse(input) {
keyEndingIndex = hasBothKeyValuePair ? equalityIndex : i;
key = input.slice(startingIndex + 1, keyEndingIndex);

// Only update value if '=' exists in the current range
if (hasBothKeyValuePair) {
value = input.slice(equalityIndex + 1, i);
}

// Add key/value pair only if the range size is greater than 1; a.k.a. contains at least "="
if (hasBothKeyValuePair || i - startingIndex > 1) {
if (hasBothKeyValuePair || key.length > 0) {
// Optimization: Replace '+' with space
if (keyHasPlus) {
key = key.replace(plusRegex, " ");
Expand All @@ -55,6 +52,8 @@ function parse(input) {
}

if (hasBothKeyValuePair) {
value = input.slice(equalityIndex + 1, i);

if (valueHasPlus) {
value = value.replace(plusRegex, " ");
}
Expand All @@ -63,12 +62,11 @@ function parse(input) {
value = fastDecode(value) || value;
}
}
const currentValue = result[key];

if (result[key] === undefined) {
if (currentValue === undefined) {
result[key] = value;
} else {
const currentValue = result[key];

// Optimization: value.pop is faster than Array.isArray(value)
if (currentValue.pop) {
currentValue.push(value);
Expand All @@ -86,31 +84,31 @@ function parse(input) {
shouldDecodeValue = false;
keyHasPlus = false;
valueHasPlus = false;
hasBothKeyValuePair = false;
}
// Check '='
else if (c === 61) {
if (equalityIndex <= startingIndex) {
equalityIndex = i;
}
// If '=' character occurs again, we should decode the input.
if (equalityIndex > startingIndex) {
else {
shouldDecodeValue = true;
} else {
equalityIndex = i;
}
}
// Check '+', and remember to replace it with empty space.
else if (c === 43) {
if (equalityIndex <= startingIndex) {
keyHasPlus = true;
} else {
if (equalityIndex > startingIndex) {
valueHasPlus = true;
} else {
keyHasPlus = true;
}
}
// Check '%' character for encoding
else if (c === 37) {
if (equalityIndex <= startingIndex) {
shouldDecodeKey = true;
} else {
if (equalityIndex > startingIndex) {
shouldDecodeValue = true;
} else {
shouldDecodeKey = true;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
"author": "Yagiz Nizipli <yagiz@nizipli.com>",
"license": "MIT",
"devDependencies": {
"@aws-sdk/querystring-builder": "^3.162.0",
"@aws-sdk/querystring-parser": "^3.162.0",
"@types/node": "^18.7.15",
"cronometro": "^1.1.2",
"http-querystring-stringify": "^2.1.0",
"qs": "^6.11.0",
"query-string": "^7.1.1",
"querystringify": "^2.2.0",
"querystringparser": "^0.1.1",
"rome": "0.9.1-next",
"vitest": "^0.23.1"
},
Expand Down
24 changes: 24 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.