|
| 1 | +'use strict'; |
| 2 | +const common = require('../common.js'); |
| 3 | +const url = require('url'); |
| 4 | +const URL = url.URL; |
| 5 | +const assert = require('assert'); |
| 6 | +const inputs = require('../fixtures/url-inputs.js').urls; |
| 7 | + |
| 8 | +const bench = common.createBenchmark(main, { |
| 9 | + type: Object.keys(inputs), |
| 10 | + method: ['legacy', 'whatwg'], |
| 11 | + n: [1e5] |
| 12 | +}); |
| 13 | + |
| 14 | +// At the time of writing, when using a passed property name to index |
| 15 | +// the object, Crankshaft would generate a LoadKeyedGeneric even when it |
| 16 | +// remains a constant in the function, so here we must use the literal |
| 17 | +// instead to get a LoadNamedField. |
| 18 | +function useLegacy(n, input) { |
| 19 | + const obj = url.parse(input); |
| 20 | + const noDead = { |
| 21 | + protocol: obj.protocol, |
| 22 | + auth: obj.auth, |
| 23 | + host: obj.host, |
| 24 | + hostname: obj.hostname, |
| 25 | + port: obj.port, |
| 26 | + pathname: obj.pathname, |
| 27 | + search: obj.search, |
| 28 | + hash: obj.hash |
| 29 | + }; |
| 30 | + // It's necessary to assign the values to an object |
| 31 | + // to avoid loop invariant code motion. |
| 32 | + bench.start(); |
| 33 | + for (var i = 0; i < n; i += 1) { |
| 34 | + noDead.protocol = obj.protocol; |
| 35 | + noDead.auth = obj.auth; |
| 36 | + noDead.host = obj.host; |
| 37 | + noDead.hostname = obj.hostname; |
| 38 | + noDead.port = obj.port; |
| 39 | + noDead.pathname = obj.pathname; |
| 40 | + noDead.search = obj.search; |
| 41 | + noDead.hash = obj.hash; |
| 42 | + } |
| 43 | + bench.end(n); |
| 44 | + return noDead; |
| 45 | +} |
| 46 | + |
| 47 | +function useWHATWG(n, input) { |
| 48 | + const obj = new URL(input); |
| 49 | + const noDead = { |
| 50 | + protocol: obj.protocol, |
| 51 | + auth: `${obj.username}:${obj.password}`, |
| 52 | + host: obj.host, |
| 53 | + hostname: obj.hostname, |
| 54 | + port: obj.port, |
| 55 | + pathname: obj.pathname, |
| 56 | + search: obj.search, |
| 57 | + hash: obj.hash |
| 58 | + }; |
| 59 | + bench.start(); |
| 60 | + for (var i = 0; i < n; i += 1) { |
| 61 | + noDead.protocol = obj.protocol; |
| 62 | + noDead.auth = `${obj.username}:${obj.password}`; |
| 63 | + noDead.host = obj.host; |
| 64 | + noDead.hostname = obj.hostname; |
| 65 | + noDead.port = obj.port; |
| 66 | + noDead.pathname = obj.pathname; |
| 67 | + noDead.search = obj.search; |
| 68 | + noDead.hash = obj.hash; |
| 69 | + } |
| 70 | + bench.end(n); |
| 71 | + return noDead; |
| 72 | +} |
| 73 | + |
| 74 | +function main(conf) { |
| 75 | + const type = conf.type; |
| 76 | + const n = conf.n | 0; |
| 77 | + const method = conf.method; |
| 78 | + |
| 79 | + const input = inputs[type]; |
| 80 | + if (!input) { |
| 81 | + throw new Error('Unknown input type'); |
| 82 | + } |
| 83 | + |
| 84 | + var noDead; // Avoid dead code elimination. |
| 85 | + switch (method) { |
| 86 | + case 'legacy': |
| 87 | + noDead = useLegacy(n, input); |
| 88 | + break; |
| 89 | + case 'whatwg': |
| 90 | + noDead = useWHATWG(n, input); |
| 91 | + break; |
| 92 | + default: |
| 93 | + throw new Error('Unknown method'); |
| 94 | + } |
| 95 | + |
| 96 | + assert.ok(noDead); |
| 97 | +} |
0 commit comments