window.MY_URL = "http://jblas:password@mycompany.com:8080/mail/inbox?msg=1234&type=unread#msg-content";
window.RX_DOMAIN = /^\w+:\/\/(?:[\w:-]+@)?([^:/#]+)/;
const url = new URL(window.MY_URL);
window.result = url.hostname;
window.result = window.MY_URL.match(window.RX_DOMAIN)[1];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
URL constructor | |
Naive regexp |
Test name | Executions per second |
---|---|
URL constructor | 319659.6 Ops/sec |
Naive regexp | 1323129.0 Ops/sec |
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark.
Benchmark Overview
The benchmark is designed to measure the performance of two approaches for extracting the domain from a URL: using the URL
constructor and using a naive regular expression (regexp) approach.
Script Preparation Code
The script preparation code sets up a test environment by defining two variables:
window.MY_URL
: a sample URL string that includes a username, password, hostname, port, path, query parameters, and an anchor.window.RX_DOMAIN
: a regular expression pattern that matches the domain part of the URL.Html Preparation Code
There is no HTML preparation code provided in this benchmark, which means that the test is focused solely on the JavaScript implementation.
Benchmark Definition
The benchmark definition specifies two test cases:
URL
constructor to parse the MY_URL
string and extract the hostname.RX_DOMAIN
) to match the domain part of the MY_URL
string.Library
In this benchmark, we don't have any external libraries being used besides the built-in URL
constructor in modern JavaScript browsers. The RX_DOMAIN
variable is a custom regular expression pattern defined within the script.
Pros and Cons
Here's a brief analysis of each approach:
Other Considerations
When considering performance benchmarks like this one, keep in mind the following factors:
Alternatives
If you're looking for alternative approaches or testing frameworks similar to MeasureThat.net, consider the following options:
These alternatives may offer additional features or flexibility in designing your own benchmarks, but MeasureThat.net's simplicity and focus on JavaScript-specific aspects make it an excellent starting point for comparing performance characteristics.