README.md (4094B)
1 # parseurl 2 3 [![NPM Version][npm-version-image]][npm-url] 4 [![NPM Downloads][npm-downloads-image]][npm-url] 5 [![Node.js Version][node-image]][node-url] 6 [![Build Status][travis-image]][travis-url] 7 [![Test Coverage][coveralls-image]][coveralls-url] 8 9 Parse a URL with memoization. 10 11 ## Install 12 13 This is a [Node.js](https://nodejs.org/en/) module available through the 14 [npm registry](https://www.npmjs.com/). Installation is done using the 15 [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally): 16 17 ```sh 18 $ npm install parseurl 19 ``` 20 21 ## API 22 23 ```js 24 var parseurl = require('parseurl') 25 ``` 26 27 ### parseurl(req) 28 29 Parse the URL of the given request object (looks at the `req.url` property) 30 and return the result. The result is the same as `url.parse` in Node.js core. 31 Calling this function multiple times on the same `req` where `req.url` does 32 not change will return a cached parsed object, rather than parsing again. 33 34 ### parseurl.original(req) 35 36 Parse the original URL of the given request object and return the result. 37 This works by trying to parse `req.originalUrl` if it is a string, otherwise 38 parses `req.url`. The result is the same as `url.parse` in Node.js core. 39 Calling this function multiple times on the same `req` where `req.originalUrl` 40 does not change will return a cached parsed object, rather than parsing again. 41 42 ## Benchmark 43 44 ```bash 45 $ npm run-script bench 46 47 > parseurl@1.3.3 bench nodejs-parseurl 48 > node benchmark/index.js 49 50 http_parser@2.8.0 51 node@10.6.0 52 v8@6.7.288.46-node.13 53 uv@1.21.0 54 zlib@1.2.11 55 ares@1.14.0 56 modules@64 57 nghttp2@1.32.0 58 napi@3 59 openssl@1.1.0h 60 icu@61.1 61 unicode@10.0 62 cldr@33.0 63 tz@2018c 64 65 > node benchmark/fullurl.js 66 67 Parsing URL "http://localhost:8888/foo/bar?user=tj&pet=fluffy" 68 69 4 tests completed. 70 71 fasturl x 2,207,842 ops/sec ±3.76% (184 runs sampled) 72 nativeurl - legacy x 507,180 ops/sec ±0.82% (191 runs sampled) 73 nativeurl - whatwg x 290,044 ops/sec ±1.96% (189 runs sampled) 74 parseurl x 488,907 ops/sec ±2.13% (192 runs sampled) 75 76 > node benchmark/pathquery.js 77 78 Parsing URL "/foo/bar?user=tj&pet=fluffy" 79 80 4 tests completed. 81 82 fasturl x 3,812,564 ops/sec ±3.15% (188 runs sampled) 83 nativeurl - legacy x 2,651,631 ops/sec ±1.68% (189 runs sampled) 84 nativeurl - whatwg x 161,837 ops/sec ±2.26% (189 runs sampled) 85 parseurl x 4,166,338 ops/sec ±2.23% (184 runs sampled) 86 87 > node benchmark/samerequest.js 88 89 Parsing URL "/foo/bar?user=tj&pet=fluffy" on same request object 90 91 4 tests completed. 92 93 fasturl x 3,821,651 ops/sec ±2.42% (185 runs sampled) 94 nativeurl - legacy x 2,651,162 ops/sec ±1.90% (187 runs sampled) 95 nativeurl - whatwg x 175,166 ops/sec ±1.44% (188 runs sampled) 96 parseurl x 14,912,606 ops/sec ±3.59% (183 runs sampled) 97 98 > node benchmark/simplepath.js 99 100 Parsing URL "/foo/bar" 101 102 4 tests completed. 103 104 fasturl x 12,421,765 ops/sec ±2.04% (191 runs sampled) 105 nativeurl - legacy x 7,546,036 ops/sec ±1.41% (188 runs sampled) 106 nativeurl - whatwg x 198,843 ops/sec ±1.83% (189 runs sampled) 107 parseurl x 24,244,006 ops/sec ±0.51% (194 runs sampled) 108 109 > node benchmark/slash.js 110 111 Parsing URL "/" 112 113 4 tests completed. 114 115 fasturl x 17,159,456 ops/sec ±3.25% (188 runs sampled) 116 nativeurl - legacy x 11,635,097 ops/sec ±3.79% (184 runs sampled) 117 nativeurl - whatwg x 240,693 ops/sec ±0.83% (189 runs sampled) 118 parseurl x 42,279,067 ops/sec ±0.55% (190 runs sampled) 119 ``` 120 121 ## License 122 123 [MIT](LICENSE) 124 125 [coveralls-image]: https://badgen.net/coveralls/c/github/pillarjs/parseurl/master 126 [coveralls-url]: https://coveralls.io/r/pillarjs/parseurl?branch=master 127 [node-image]: https://badgen.net/npm/node/parseurl 128 [node-url]: https://nodejs.org/en/download 129 [npm-downloads-image]: https://badgen.net/npm/dm/parseurl 130 [npm-url]: https://npmjs.org/package/parseurl 131 [npm-version-image]: https://badgen.net/npm/v/parseurl 132 [travis-image]: https://badgen.net/travis/pillarjs/parseurl/master 133 [travis-url]: https://travis-ci.org/pillarjs/parseurl