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

speedup array work by using push instead of concat #185

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/parse.js
Expand Up @@ -32,8 +32,9 @@ var parseValues = function parseQueryStringValues(str, options) {
key = options.decoder(part.slice(0, pos));
val = options.decoder(part.slice(pos + 1));
}

if (has.call(obj, key)) {
obj[key] = [].concat(obj[key]).concat(val);
obj[key] = utils.combine(obj[key], val);
} else {
obj[key] = val;
}
Expand Down
21 changes: 21 additions & 0 deletions lib/utils.js
Expand Up @@ -178,3 +178,24 @@ exports.isBuffer = function (obj) {

return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
};

exports.combine = function combine(a, b) {
// we always use both of these, so, let's calculate them now
var firstIsArray = Array.isArray(a);
var secondIsArray = Array.isArray(b);

// mutate `a` to append `b` and then return it
if (firstIsArray) {
secondIsArray ? a.push.apply(a, b) : a.push(b);
Copy link
Owner

Choose a reason for hiding this comment

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

It's probably a good idea to merge in utils.combine with concat first into master, and then have the optimization PR be separate.

return a;
}

// mutate `b` to prepend `a` and then return it
if (secondIsArray) {
b.unshift(a);
return b;
}

// neither are arrays, so, create a new array with both
return [a, b];
};