<style>
#container {
width: 1000px;
height: 1000px;
}
#container * {
display: flex;
flex: 1 1;
background: red;
border: 1px solid black;
min-width: 0;
min-height: 0;
width: 100%;
height: 100%;
}
#container .tick {
flex-direction: row;
}
#container .tock {
flex-direction: column;
}
</style>
<div id="container"></div>
const container = document.getElementById("container");
const depth = 9;
const childrenPerLevel = 3;
/**
* @param {HTMLElement} parent
* @param {boolean} isTick
*/
const addElement = (parent, isTick) => {
const el = document.createElement("div");
el.classList.add(isTick ? "tick" : "tock");
parent.appendChild(el);
return el;
};
let currLevel = [container];
let nextLevel = [];
for (let i = 0; i < depth; i++) {
for (const parent of currLevel) {
for (let j = 0; j < childrenPerLevel; j++) {
nextLevel.push(addElement(parent, i % 2 === 0));
}
}
currLevel = nextLevel;
nextLevel = [];
}
const n = 1000;
const nsq = Math.floor(Math.sqrt(n));
const arr = Array(n).fill(0).map((v,i)=>i);
arr.map((v,i)=>document.elementFromPoint(i%nsq, Math.floor(i/nsq)));
console.log(".");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
elementFromPoint | |
second |
Test name | Executions per second |
---|---|
elementFromPoint | 488.3 Ops/sec |
second | 433549.9 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
Benchmark Definition
The provided JSON represents the benchmark definition for two test cases: elementFromPoint1
and second
.
elementFromPoint1
: This test case measures the performance of the document.elementFromPoint()
method, which returns the element at the specified coordinates within an HTML document. The script preparation code creates a nested DOM structure with a container element and adds multiple child elements to it using the addElement
function.second
: This test case simply logs a dot (.
) to the console.Options Compared
The benchmark compares two options for executing the tests:
Pros and Cons
Library Usage
In the elementFromPoint1
test case, the library used is:
Special JS Features or Syntax
The elementFromPoint()
method uses special JavaScript features:
document.elementFromPoint()
: This method returns the element at the specified coordinates within an HTML document.Math.floor(Math.sqrt(n))
: This expression calculates the integer part of the square root of a number using the Math.sqrt()
function and then rounding down to the nearest integer with Math.floor()
.Other Alternatives
If you want to create your own JavaScript microbenchmarking framework, here are some alternatives:
These alternatives offer different features, ease of use, and flexibility compared to MeasureThat.net. However, MeasureThat.net remains a valuable resource for testing and comparing JavaScript performance in a controlled environment.