<script type='module'>
import qs from 'https://cdn.skypack.dev/query-string';
window.qs = qs
</script>
window.string = 'hola=mundo&soy=david'
window.parseQuery = (query) => {
const searchParams = new URLSearchParams(query);
const result = {}
for (const key in searchParams.keys()) {
const items = searchParams.getAll(key)
result[key] = items.length - 1 ? items : items[0]
}
return searchParams;
};
qs.parse(string)
parseQuery(string)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
query-string | |
URLSearchParams |
Test name | Executions per second |
---|---|
query-string | 112308.4 Ops/sec |
URLSearchParams | 107696.7 Ops/sec |
Overview
The provided benchmark compares two approaches to parse query strings in JavaScript: query-string
and URLSearchParams
. The test measures the performance of each approach by executing the parsing function on a sample string.
Library Descriptions
Comparison of Approaches
The two approaches differ in their implementation:
qs.parse
) that iterates through the query string, splitting it into individual key-value pairs and handling cases where there are multiple values for a single key.URL
object, which is also used in browsers for parsing URLs.Pros and Cons
query-string
since it's a standardized API.Special JavaScript Features/Syntax
The benchmark uses special JavaScript features:
parseQuery
function to define an anonymous function that takes two arguments (the query string and returns an object).query-string
library in the HTML preparation code.Other Alternatives
If you're looking for alternative libraries to parse query strings, some popular options include:
query-string
.querystring
library from Node.js.Keep in mind that each alternative has its own strengths and weaknesses, so it's essential to choose the one that best fits your specific needs.