Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance: string methods instead of new URL #79

Open
DarkCat09 opened this issue Feb 14, 2024 · 1 comment
Open

Performance: string methods instead of new URL #79

DarkCat09 opened this issue Feb 14, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@DarkCat09
Copy link
Member

We'll get a really good performance boost if we replace new URL, which is used too much often as I see, with string methods. On the other hand, manually parsing URL in some cases is not simple and concise.

Benchmark code
const Benchmarkify = require("benchmarkify")

const benchmark = new Benchmarkify("Benchmark", { chartImage: true }).printHeader()

benchmark.createSuite("URL convert", { time: 10000 })

  .add("URL object", () => {
                const url = new URL("https://dc09.ru/posts/fediverse#comments")
                const hash = url.hash
                url.hash = ""
                return encodeURIComponent(url.toString()) + hash
        })

        .ref("String methods", () => {
                let url = "https://dc09.ru/posts/fediverse#comments"
                const hashIdx = url.indexOf("#")
                if (hashIdx != -1) {
                        return encodeURIComponent(url.substring(0, hashIdx)) + url.substring(hashIdx)
                }
                else {
                        return encodeURIComponent(url)
                }
        })

benchmark.run()
Benchmark results
Platform info:
==============
   Linux 6.7.4-artix1-1 x64
   Node.JS: 21.6.1
   V8: 11.8.172.17-node.19
   CPU: Intel(R) Core(TM) i5-10400 CPU @ 2.90GHz × 12
   Memory: 30 GB

Suite: URL convert
==================

✔ URL object         1 176 230 ops/sec
✔ String methods     4 825 419 ops/sec

   URL object           -75,62%   (1 176 230 ops/sec)   (avg: 850ns)
   String methods (#)        0%   (4 825 419 ops/sec)   (avg: 207ns)

┌────────────────┬────────────────────────────────────────────────────┐
│ URL object     │ ████████████                                       │
├────────────────┼────────────────────────────────────────────────────┤
│ String methods │ ██████████████████████████████████████████████████ │
└────────────────┴────────────────────────────────────────────────────┘
@DarkCat09 DarkCat09 added the enhancement New feature or request label Feb 14, 2024
@artegoser
Copy link
Member

We can use fast-url-parser.

Benchmark code
const Benchmarkify = require("benchmarkify");
const furl = require("fast-url-parser");

const benchmark = new Benchmarkify("Benchmark", {
  chartImage: true,
}).printHeader();

benchmark
  .createSuite("URL convert", { time: 10000 })
  .add("URL object", () => {
    const url = new URL("https://dc09.ru/posts/fediverse#comments");
    const hash = url.hash;
    url.hash = "";
    return encodeURIComponent(url.toString()) + hash;
  })
  .ref("Fast URL parser", () => {
    const url = furl.parse("https://dc09.ru/posts/fediverse#comments");
    const hash = url.hash;
    url.hash = "";
    return encodeURIComponent(url.toString()) + hash;
  })
  .ref("String methods", () => {
    let url = "https://dc09.ru/posts/fediverse#comments";
    const hashIdx = url.indexOf("#");
    if (hashIdx != -1) {
      return (
        encodeURIComponent(url.substring(0, hashIdx)) + url.substring(hashIdx)
      );
    } else {
      return encodeURIComponent(url);
    }
  });

benchmark.run();
Benchmark results
Suite: URL convert
==================

✔ URL object            930,889 ops/sec
✔ Fast URL parser     2,423,227 ops/sec
✔ String methods      3,738,100 ops/sec

   URL object             -75.1%    (930,889 ops/sec)   (avg: 1μs)
   Fast URL parser       -35.17%   (2,423,227 ops/sec)   (avg: 412ns)
   String methods (#)         0%   (3,738,100 ops/sec)   (avg: 267ns)

┌─────────────────┬────────────────────────────────────────────────────┐
│ URL object      │ ████████████                                       │
├─────────────────┼────────────────────────────────────────────────────┤
│ Fast URL parser │ ████████████████████████████████                   │
├─────────────────┼────────────────────────────────────────────────────┤
│ String methods  │ ██████████████████████████████████████████████████ │
└─────────────────┴────────────────────────────────────────────────────┘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants