document.location.pathname;
document.URL;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
document.location.pathname | |
document.URL |
Test name | Executions per second |
---|---|
document.location.pathname | 1346660.5 Ops/sec |
document.URL | 3990872.2 Ops/sec |
Let's break down what's being tested in this JavaScript microbenchmark.
What is being tested?
The benchmark compares two different ways to access the URL of a webpage: document.location.pathname
and document.URL
. The goal is to determine which method is faster, more efficient, or has some other desirable property.
Options compared:
document.location.pathname
: This returns the path part of the current URL. For example, if the URL is /path/to/resource
, it will return /path/to
.document.URL
: This returns the entire URL string. For example, if the URL is /path/to/resource
, it will return /path/to/resource
.Pros and Cons:
document.location.pathname
:document.URL
:Library and special JS feature:
There are no libraries mentioned in this benchmark. However, note that some modern browsers have a security feature called same-origin policy, which might affect how document.location
behaves when accessing certain properties.
Special JS feature or syntax:
This benchmark does not use any special JavaScript features or syntax beyond standard ECMAScript (JS) syntax.
Other alternatives:
If you wanted to compare these two approaches in a different way, you might consider:
window
object with controlled URL values.However, the current approach allows users to easily compare these two common ways to access the URL in JavaScript, making it a straightforward and accessible benchmark.