3 ms·
Truer words! I did a quick microbench of a few implementations of this and found that Resig's implementation is far from the best performance possible. PSA: Mic
by rezonant 6y ago
Truer words! I did a quick microbench of a few implementations of this and found that Resig's implementation is far from the best performance possible. PSA: Microbenchmarks are not indicative of real world performance
Find the code here: https://gist.github.com/rezonant/639c67db5bd6503e8f022291b918bb62 https://gist.github.com/rezonant/639c67db5bd6503e8f022291b91...
Results on my system (Core i7 7700, Node.js 10.15.3, Windows 10 2004):
----
Comparing 7 implementations, 1000000 repetitions each
short input:
resig: 2920ms
resigModernized: 2369ms
fullyFunctional: 2817ms
functionalHybrid: 2352ms
mapReduce: 5002ms
splitmap: 1490ms
compressURL: 6064ms
long input, few keys:
resig: 28664ms
resigModernized: 23087ms
fullyFunctional: 17509ms
functionalHybrid: 18431ms
mapReduce: 46959ms
splitmap: 14791ms
compressURL: 24485ms
long input, many keys:
resig: 17468ms
resigModernized: 15049ms
fullyFunctional: 15781ms
functionalHybrid: 13768ms
mapReduce: 30352ms
splitmap: 12959ms
compressURL: 34180ms
----
The winner (according to this crude benchmark) is this implementation:
function splitmap(data){
let q = new Map();
for (let [key, value] of data.split(/&/g).map(x => x.split(/=/))) {
q.set(key, `${q.has(key) ? q.get(key) + ',' : ''}${value}`);
}
let ret = "";
for (let [ key, value ] of q)
ret = `${ret ? ret + '&' : ''}${key}=${value}`;
return ret;
}
...but I'm sure folks can come up with something faster
EDIT: The splitmap() implementation will fail on key=value=foo, cutting off the extra "=foo", though if you are expecting valid URL-encoded params then this might be an acceptable limitation.