mirror of https://github.com/lukechilds/node.git
Browse Source
- add some benchmarks for URLSearchParams - change URLSearchParams backing store to an array - add custom inspection for URLSearchParams and its iterators PR-URL: https://github.com/nodejs/node/pull/10399 Reviewed-By: James M Snell <jasnell@gmail.com>v6
committed by
Italo A. Casas
6 changed files with 369 additions and 43 deletions
@ -0,0 +1,60 @@ |
|||||
|
'use strict'; |
||||
|
const common = require('../common.js'); |
||||
|
const assert = require('assert'); |
||||
|
const URLSearchParams = new (require('url').URL)('a:').searchParams.constructor; |
||||
|
|
||||
|
const bench = common.createBenchmark(main, { |
||||
|
method: ['forEach', 'iterator'], |
||||
|
n: [1e6] |
||||
|
}); |
||||
|
|
||||
|
const str = 'one=single&two=first&three=first&two=2nd&three=2nd&three=3rd'; |
||||
|
|
||||
|
function forEach(n) { |
||||
|
const params = new URLSearchParams(str); |
||||
|
const noDead = []; |
||||
|
const cb = (val, key) => { |
||||
|
noDead[0] = key; |
||||
|
noDead[1] = val; |
||||
|
}; |
||||
|
|
||||
|
bench.start(); |
||||
|
for (var i = 0; i < n; i += 1) |
||||
|
params.forEach(cb); |
||||
|
bench.end(n); |
||||
|
|
||||
|
assert.strictEqual(noDead[0], 'three'); |
||||
|
assert.strictEqual(noDead[1], '3rd'); |
||||
|
} |
||||
|
|
||||
|
function iterator(n) { |
||||
|
const params = new URLSearchParams(str); |
||||
|
const noDead = []; |
||||
|
|
||||
|
bench.start(); |
||||
|
for (var i = 0; i < n; i += 1) |
||||
|
for (var pair of params) { |
||||
|
noDead[0] = pair[0]; |
||||
|
noDead[1] = pair[1]; |
||||
|
} |
||||
|
bench.end(n); |
||||
|
|
||||
|
assert.strictEqual(noDead[0], 'three'); |
||||
|
assert.strictEqual(noDead[1], '3rd'); |
||||
|
} |
||||
|
|
||||
|
function main(conf) { |
||||
|
const method = conf.method; |
||||
|
const n = conf.n | 0; |
||||
|
|
||||
|
switch (method) { |
||||
|
case 'forEach': |
||||
|
forEach(n); |
||||
|
break; |
||||
|
case 'iterator': |
||||
|
iterator(n); |
||||
|
break; |
||||
|
default: |
||||
|
throw new Error('Unknown method'); |
||||
|
} |
||||
|
} |
@ -0,0 +1,31 @@ |
|||||
|
'use strict'; |
||||
|
const common = require('../common.js'); |
||||
|
const URLSearchParams = new (require('url').URL)('a:').searchParams.constructor; |
||||
|
|
||||
|
const inputs = { |
||||
|
noencode: 'foo=bar&baz=quux&xyzzy=thud', |
||||
|
// multicharsep: 'foo=bar&&&&&&&&&&baz=quux&&&&&&&&&&xyzzy=thud',
|
||||
|
multicharsep: '&&&&&&&&&&&&&&&&&&&&&&&&&&&&', |
||||
|
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', |
||||
|
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' |
||||
|
}; |
||||
|
|
||||
|
const bench = common.createBenchmark(main, { |
||||
|
type: Object.keys(inputs), |
||||
|
n: [1e5] |
||||
|
}); |
||||
|
|
||||
|
function main(conf) { |
||||
|
const input = inputs[conf.type]; |
||||
|
const n = conf.n | 0; |
||||
|
|
||||
|
var i; |
||||
|
bench.start(); |
||||
|
for (i = 0; i < n; i++) |
||||
|
new URLSearchParams(input); |
||||
|
bench.end(n); |
||||
|
} |
@ -0,0 +1,58 @@ |
|||||
|
'use strict'; |
||||
|
const common = require('../common.js'); |
||||
|
const URLSearchParams = new (require('url').URL)('a:').searchParams.constructor; |
||||
|
|
||||
|
const bench = common.createBenchmark(main, { |
||||
|
method: ['get', 'getAll', 'has'], |
||||
|
param: ['one', 'two', 'three', 'nonexistent'], |
||||
|
n: [1e6] |
||||
|
}); |
||||
|
|
||||
|
const str = 'one=single&two=first&three=first&two=2nd&three=2nd&three=3rd'; |
||||
|
|
||||
|
function get(n, param) { |
||||
|
const params = new URLSearchParams(str); |
||||
|
|
||||
|
bench.start(); |
||||
|
for (var i = 0; i < n; i += 1) |
||||
|
params.get(param); |
||||
|
bench.end(n); |
||||
|
} |
||||
|
|
||||
|
function getAll(n, param) { |
||||
|
const params = new URLSearchParams(str); |
||||
|
|
||||
|
bench.start(); |
||||
|
for (var i = 0; i < n; i += 1) |
||||
|
params.getAll(param); |
||||
|
bench.end(n); |
||||
|
} |
||||
|
|
||||
|
function has(n, param) { |
||||
|
const params = new URLSearchParams(str); |
||||
|
|
||||
|
bench.start(); |
||||
|
for (var i = 0; i < n; i += 1) |
||||
|
params.has(param); |
||||
|
bench.end(n); |
||||
|
} |
||||
|
|
||||
|
function main(conf) { |
||||
|
const method = conf.method; |
||||
|
const param = conf.param; |
||||
|
const n = conf.n | 0; |
||||
|
|
||||
|
switch (method) { |
||||
|
case 'get': |
||||
|
get(n, param); |
||||
|
break; |
||||
|
case 'getAll': |
||||
|
getAll(n, param); |
||||
|
break; |
||||
|
case 'has': |
||||
|
has(n, param); |
||||
|
break; |
||||
|
default: |
||||
|
throw new Error('Unknown method'); |
||||
|
} |
||||
|
} |
@ -0,0 +1,35 @@ |
|||||
|
'use strict'; |
||||
|
const common = require('../common.js'); |
||||
|
const Buffer = require('buffer').Buffer; |
||||
|
const URLSearchParams = new (require('url').URL)('a:').searchParams.constructor; |
||||
|
|
||||
|
const inputs = { |
||||
|
noencode: 'foo=bar&baz=quux&xyzzy=thud', |
||||
|
// multicharsep: 'foo=bar&&&&&&&&&&baz=quux&&&&&&&&&&xyzzy=thud',
|
||||
|
multicharsep: '&&&&&&&&&&&&&&&&&&&&&&&&&&&&', |
||||
|
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', |
||||
|
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' |
||||
|
}; |
||||
|
|
||||
|
const bench = common.createBenchmark(main, { |
||||
|
type: Object.keys(inputs), |
||||
|
n: [1e5] |
||||
|
}); |
||||
|
|
||||
|
function main(conf) { |
||||
|
const input = inputs[conf.type]; |
||||
|
const n = conf.n | 0; |
||||
|
|
||||
|
const params = new URLSearchParams(input); |
||||
|
|
||||
|
bench.start(); |
||||
|
// Using Buffer.from to prevent JS version from cheating with ropes instead
|
||||
|
// of strings
|
||||
|
for (var i = 0; i < n; i += 1) |
||||
|
Buffer.from(params.toString()); |
||||
|
bench.end(n); |
||||
|
} |
@ -0,0 +1,30 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
require('../common'); |
||||
|
const assert = require('assert'); |
||||
|
const util = require('util'); |
||||
|
const URL = require('url').URL; |
||||
|
|
||||
|
// Until we export URLSearchParams
|
||||
|
const m = new URL('http://example.org'); |
||||
|
const URLSearchParams = m.searchParams.constructor; |
||||
|
|
||||
|
const sp = new URLSearchParams('?a=a&b=b&b=c'); |
||||
|
assert.strictEqual(util.inspect(sp), |
||||
|
"URLSearchParams { 'a' => 'a', 'b' => 'b', 'b' => 'c' }"); |
||||
|
assert.strictEqual(util.inspect(sp.keys()), |
||||
|
"URLSearchParamsIterator { 'a', 'b', 'b' }"); |
||||
|
assert.strictEqual(util.inspect(sp.values()), |
||||
|
"URLSearchParamsIterator { 'a', 'b', 'c' }"); |
||||
|
|
||||
|
const iterator = sp.entries(); |
||||
|
assert.strictEqual(util.inspect(iterator), |
||||
|
"URLSearchParamsIterator { [ 'a', 'a' ], [ 'b', 'b' ], " + |
||||
|
"[ 'b', 'c' ] }"); |
||||
|
iterator.next(); |
||||
|
assert.strictEqual(util.inspect(iterator), |
||||
|
"URLSearchParamsIterator { [ 'b', 'b' ], [ 'b', 'c' ] }"); |
||||
|
iterator.next(); |
||||
|
iterator.next(); |
||||
|
assert.strictEqual(util.inspect(iterator), |
||||
|
'URLSearchParamsIterator { }'); |
Loading…
Reference in new issue