0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/benchmark/url/legacy-vs-whatwg-url-get-prop.js
Vse Mozhet Byt 22aa3d4899 benchmark: reduce string concatenations
PR-URL: https://github.com/nodejs/node/pull/12455
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-04-20 04:46:37 +03:00

98 lines
2.4 KiB
JavaScript

'use strict';
const common = require('../common.js');
const url = require('url');
const URL = url.URL;
const assert = require('assert');
const inputs = require('../fixtures/url-inputs.js').urls;
const bench = common.createBenchmark(main, {
type: Object.keys(inputs),
method: ['legacy', 'whatwg'],
n: [1e5]
});
// At the time of writing, when using a passed property name to index
// the object, Crankshaft would generate a LoadKeyedGeneric even when it
// remains a constant in the function, so here we must use the literal
// instead to get a LoadNamedField.
function useLegacy(n, input) {
var obj = url.parse(input);
var noDead = {
protocol: obj.protocol,
auth: obj.auth,
host: obj.host,
hostname: obj.hostname,
port: obj.port,
pathname: obj.pathname,
search: obj.search,
hash: obj.hash
};
// It's necessary to assign the values to an object
// to avoid loop invariant code motion.
bench.start();
for (var i = 0; i < n; i += 1) {
noDead.protocol = obj.protocol;
noDead.auth = obj.auth;
noDead.host = obj.host;
noDead.hostname = obj.hostname;
noDead.port = obj.port;
noDead.pathname = obj.pathname;
noDead.search = obj.search;
noDead.hash = obj.hash;
}
bench.end(n);
return noDead;
}
function useWHATWG(n, input) {
var obj = new URL(input);
var noDead = {
protocol: obj.protocol,
auth: `${obj.username}:${obj.password}`,
host: obj.host,
hostname: obj.hostname,
port: obj.port,
pathname: obj.pathname,
search: obj.search,
hash: obj.hash
};
bench.start();
for (var i = 0; i < n; i += 1) {
noDead.protocol = obj.protocol;
noDead.auth = `${obj.username}:${obj.password}`;
noDead.host = obj.host;
noDead.hostname = obj.hostname;
noDead.port = obj.port;
noDead.pathname = obj.pathname;
noDead.search = obj.search;
noDead.hash = obj.hash;
}
bench.end(n);
return noDead;
}
function main(conf) {
const type = conf.type;
const n = conf.n | 0;
const method = conf.method;
const input = inputs[type];
if (!input) {
throw new Error('Unknown input type');
}
var noDead; // Avoid dead code elimination.
switch (method) {
case 'legacy':
noDead = useLegacy(n, input);
break;
case 'whatwg':
noDead = useWHATWG(n, input);
break;
default:
throw new Error('Unknown method');
}
assert.ok(noDead);
}