<div id="foo" data-bar="bar"></div>
const element = document.getElementById("foo");
const ob = {};
ob.data = element.dataset.bar;
var i = 2000;
while (i--) {
console.log(ob.data);
}
const element = document.getElementById("foo");
var i = 2000;
while (i--) {
console.log(element.dataset.bar);
}
const element = document.getElementById("foo");
const bar = element.dataset.bar;
var i = 2000;
while (i--) {
console.log(bar);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
object entry | |
dataset | |
variable |
Test name | Executions per second |
---|---|
object entry | 44.6 Ops/sec |
dataset | 41.0 Ops/sec |
variable | 44.0 Ops/sec |
Let's dive into explaining the provided benchmark.
What is being tested?
The test measures how fast JavaScript code runs in three different scenarios:
object
): The code creates an empty object ob
, assigns the value of element.dataset.bar
to its data
property, and then logs the data
property repeatedly using a while loop.variable
): The code directly accesses the value of element.dataset.bar
through the bar
variable, and then logs it repeatedly using a while loop.dataset
): This is similar to the "variable" scenario, but the data is retrieved directly from element.dataset.bar
.Options compared
The three options being compared are:
These options represent different ways of accessing and manipulating data in JavaScript.
Pros and cons of each approach
object
):variable
)dataset
):Library used
In this benchmark, no specific library is explicitly mentioned. However, it's likely that the element
object is an HTML element created using the DOM (Document Object Model) API.
Special JavaScript features or syntax
This benchmark does not explicitly use any special JavaScript features or syntax beyond standard ES5 syntax. However, it does rely on modern web technologies like HTML5 and CSS3 for creating the test environment.
Other alternatives
If you were to implement this benchmark yourself, you might consider adding more scenarios, such as:
Keep in mind that the specific alternatives will depend on your goals and requirements.
I hope this explanation helps!