<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 = 12;
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 = 1_000;
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)));
const n = 10_000;
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)));
const n = 100_000;
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)));
const n = 1_000_000;
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)));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
elementFromPoint 1K | |
elementFromPoint 10K | |
elementFromPoint 100K | |
elementFromPoint 1M |
Test name | Executions per second |
---|---|
elementFromPoint 1K | 542.2 Ops/sec |
elementFromPoint 10K | 45.5 Ops/sec |
elementFromPoint 100K | 9.0 Ops/sec |
elementFromPoint 1M | 2.3 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript microbenchmarking framework called MeasureThat.net. The benchmark tests the performance of the document.elementFromPoint()
method, which returns an element at a specific coordinate.
Benchmark Definition JSON Analysis
The Benchmark Definition JSON contains three main elements:
addElement
that creates child elements (div tags) inside the parent element, which will be used to simulate a hierarchical structure.Benchmark Definition
property that contains a JavaScript snippet that will be executed.Test Case Analysis
Each test case generates an array of coordinates and iterates over it using the map()
method, calling document.elementFromPoint()
for each coordinate. The resulting elements are not collected or used in any way; they are simply discarded.
Library and Functionality
No external libraries are explicitly mentioned in the Benchmark Definition JSON. However, the elementFromPoint()
function is a built-in JavaScript method that returns an element at a specific point within a document.
Special JS Features
There are no special JavaScript features or syntax used in this benchmark.
Performance Comparison
The benchmark compares the performance of the document.elementFromPoint()
method for different array sizes (1K, 10K, 100K, and 1M). The results will likely show that the execution time increases with the size of the input array.
Pros and Cons of Different Approaches
There are two main approaches used in this benchmark:
Approach 1: Using a recursive function to create a hierarchical structure (Script Preparation Code).
Pros:
Cons:
Approach 2: Using an array-based approach with map()
(Individual Test Cases).
Pros:
Cons:
Other Alternatives
If you were to rewrite this benchmark, you could consider using other approaches or variations, such as:
document.elementFromPoint()
calls.Keep in mind that each alternative approach will introduce its own trade-offs and potential pitfalls, so be sure to carefully evaluate the pros and cons before making any changes.