Skip to content

Benchmark combo - #189

Merged
ljharb merged 2 commits into
ljharb:mainfrom
elidoran:benchmark-combo
Dec 2, 2026
Merged

ljharb merged 2 commits into
ljharb:mainfrom
elidoran:benchmark-combo

Conversation

@elidoran

Copy link
Copy Markdown
Contributor

See #185 and #187

This PR applies the benchmarking stuff first. Then moves the combo stuff to the utils function with the original implementation. Then replaces it with the new implementation.

Run benchmark with node benchmark/arraying.js.

There's a variety of ways to setup how to run benchmarking scripts as you add more. Could use npm scripts entry per each benchmark. Or, make an index file in there which accepts a subcommand for which one to run so there's only one npm run script. I leave those decisions up to you.

Adding the new implementation is last so you can peel it off from the PR if you'd like.

@ljharb ljharb left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could 3f1fc4b add tests for utils.combine, in particular ensuring that a and b are not mutated? That way, if that behavior needs to change in the "new implementation" commit, that commit can also alter the tests - so that the scope of the changes is apparent.

@elidoran

Copy link
Copy Markdown
Contributor Author

I think the best place to let everyone know an internal change occurred is the CHANGELOG.

It doesn't seem a good use of time to write a test for something which doesn't need to exist and then immediately remove that test for a new implementation.

Mutation is the correct operation to append a value to an internally created and used array. That's why the push() function exists. User's only care about receiving the array for that param after parsing. It doesn't matter to them what the libary did to produce that array.

Ultimately, it's your library. Feel free to test how you like and accept, revise, or reject my PR's because the library's functionality is your responsibility.

I recommend you take the benchmarking out for a spin. Adapt it to compare current implementations against dev versions and try out some new stuff.

For example, you may consider altering parseObject() to build up the object from the tail of the array instead so it pop()'s off the end instead of shifting the array. Or, check out the use of concat() in stringify where push() is more appropriate.

It's an adventure. Have fun.

@ljharb

ljharb commented Dec 22, 2016 •

Copy link
Copy Markdown
Owner

I'm suggesting that this lets me, the reviewer, know what changes the PR is making - and the changelog is written by manually auditing the PR.

The other purpose of writing the tests is that it lets me merge in the first commit immediately, while it will take much more time for me to evaluate the rest of the PR. If you're not willing to take the time, that's entirely up to you.

Querystring parsing is not a use case that requires high performance, because there's only one query string per request, so there's no urgency to accept a performance improvement - especially if it changes observable semantics by adding mutation to a code path (which is far worse a thing than "slow")

Thanks for your contribution so far; I'll leave this open in case you decide to put in the time in the future.

@ljharb
ljharb force-pushed the benchmark-combo branch 2 times, most recently from 6688a4e to cb38173 Compare November 24, 2018 01:53
@ljharb

ljharb commented Nov 24, 2018

Copy link
Copy Markdown
Owner

@elidoran I've improved the tests and pulled the combine util into master directly, and rebased this branch. Tests are now failing on this PR.

If you're still interested in seeing the performance of combine improve, I'd love to see you update this PR to get the tests passing.

ljharb and others added 2 commits November 30, 2026 21:37
… unspreadable arrays

These pin what `[].concat(a, b)` produces,
so a faster `combine` cannot change it.
…ng to spread

`[].concat(a, b)` is far slower than an array literal.
`combine` now builds `[a, b]` directly when both are primitives,
as with the first duplicate of a plain key.
Every `a[]=x` calls `combine([], x)`,
so `parseObject` builds `[x]` itself for a primitive `x` when `arrayLimit` allows it,
since only there is the empty array known to be qs's own,
and an array from elsewhere (eg, from a decoder) could be a proxy,
or opt out of spreading.
This parses array-heavy queries about 3-6% faster.

Co-authored-by: Eli Doran <[email protected]>
Co-authored-by: Jordan Harband <[email protected]>
@ljharb
ljharb marked this pull request as ready for review December 1, 2026 06:09

@ljharb ljharb left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, and sorry this sat so long! Most of it has since landed in other forms: #527 added a benchmark suite, and #652 made combining duplicate keys linear without mutating combine's arguments. But your "neither are arrays, so create a new array with both" case still held up, so I rebased this down to that idea: combine now builds [a, b] directly when both are primitives, and a[]=x builds [x] directly instead of calling concat. I added tests first that pin what concat produces, including for an empty array a decoder returns, which is why the empty-array shortcut only applies where qs creates that array itself. It parses array-heavy queries about 3-6% faster, and a differential fuzz against main found no differences in 1M cases.

@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.89%. Comparing base (6a45295) to head (a291518).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #189   +/-   ##
=======================================
  Coverage   99.89%   99.89%           
=======================================
  Files           9        9           
  Lines        3727     3781   +54     
  Branches      375      381    +6     
=======================================
+ Hits         3723     3777   +54     
  Misses          4        4           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ljharb
ljharb merged commit a291518 into ljharb:main Dec 2, 2026
455 of 470 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants