mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 21:58:48 +02:00
querystring: improve parse() performance
This commit improves parse() performance by ~20-200% with the various querystring-parse benchmarks. Some optimization strategies used in this commit include: * Combining multiple searches (for '&', '=', and '+') on the same string into a single loop * Avoiding string.split() * Minimizing creation of temporary strings * Avoiding string decoding if no encoded bytes were found and the default string decoder is being used PR-URL: https://github.com/nodejs/node/pull/5012 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
90451a67ca
commit
a2a69a2b63
3 changed files with 191 additions and 62 deletions
|
@ -3,7 +3,13 @@ var querystring = require('querystring');
|
|||
var v8 = require('v8');
|
||||
|
||||
var bench = common.createBenchmark(main, {
|
||||
type: ['noencode', 'encodemany', 'encodelast', 'multivalue'],
|
||||
type: ['noencode',
|
||||
'multicharsep',
|
||||
'encodemany',
|
||||
'encodelast',
|
||||
'multivalue',
|
||||
'multivaluemany',
|
||||
'manypairs'],
|
||||
n: [1e6],
|
||||
});
|
||||
|
||||
|
@ -13,22 +19,38 @@ function main(conf) {
|
|||
|
||||
var inputs = {
|
||||
noencode: 'foo=bar&baz=quux&xyzzy=thud',
|
||||
multicharsep: 'foo=bar&&&&&&&&&&baz=quux&&&&&&&&&&xyzzy=thud',
|
||||
encodemany: '%66%6F%6F=bar&%62%61%7A=quux&xyzzy=%74h%75d',
|
||||
encodelast: 'foo=bar&baz=quux&xyzzy=thu%64',
|
||||
multivalue: 'foo=bar&foo=baz&foo=quux&quuy=quuz'
|
||||
multivalue: 'foo=bar&foo=baz&foo=quux&quuy=quuz',
|
||||
multivaluemany: 'foo=bar&foo=baz&foo=quux&quuy=quuz&foo=abc&foo=def&' +
|
||||
'foo=ghi&foo=jkl&foo=mno&foo=pqr&foo=stu&foo=vwxyz',
|
||||
manypairs: 'a&b&c&d&e&f&g&h&i&j&k&l&m&n&o&p&q&r&s&t&u&v&w&x&y&z'
|
||||
};
|
||||
var input = inputs[type];
|
||||
|
||||
// Force-optimize querystring.parse() so that the benchmark doesn't get
|
||||
// disrupted by the optimizer kicking in halfway through.
|
||||
for (var name in inputs)
|
||||
querystring.parse(inputs[name]);
|
||||
|
||||
v8.setFlagsFromString('--allow_natives_syntax');
|
||||
eval('%OptimizeFunctionOnNextCall(querystring.parse)');
|
||||
|
||||
bench.start();
|
||||
for (var i = 0; i < n; i += 1)
|
||||
if (type !== 'multicharsep') {
|
||||
querystring.parse(input);
|
||||
bench.end(n);
|
||||
eval('%OptimizeFunctionOnNextCall(querystring.parse)');
|
||||
querystring.parse(input);
|
||||
} else {
|
||||
querystring.parse(input, '&&&&&&&&&&');
|
||||
eval('%OptimizeFunctionOnNextCall(querystring.parse)');
|
||||
querystring.parse(input, '&&&&&&&&&&');
|
||||
}
|
||||
|
||||
if (type !== 'multicharsep') {
|
||||
bench.start();
|
||||
for (var i = 0; i < n; i += 1)
|
||||
querystring.parse(input);
|
||||
bench.end(n);
|
||||
} else {
|
||||
bench.start();
|
||||
for (var i = 0; i < n; i += 1)
|
||||
querystring.parse(input, '&&&&&&&&&&');
|
||||
bench.end(n);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue