1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-11-11 07:38:58 +01:00
CanvasBlocker/test/domRectTest.js

146 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-09-05 08:21:22 +02:00
(function(){
"use strict";
function byteArrayToHex(arrayBuffer){
2019-12-16 19:27:28 +01:00
const chunks = [];
2018-09-05 08:21:22 +02:00
(new Uint32Array(arrayBuffer)).forEach(function(num){
chunks.push(num.toString(16));
});
return chunks.map(function(chunk){
return "0".repeat(8 - chunk.length) + chunk;
}).join("");
}
const container = document.getElementById("tests");
const iframe = document.getElementById("iframe");
const template = document.querySelector(".test");
template.parentElement.removeChild(template);
function getElements(){
const doc = iframe.contentDocument;
2019-03-16 00:52:28 +01:00
return Array.from(doc.querySelectorAll(".testRect"));
2018-09-05 08:21:22 +02:00
}
2019-11-28 01:26:35 +01:00
function formatNumber(number){
const str = number.toString();
return "<span class=small>" + str.substring(0, str.length - 2) + "</span>" +
str.substring(str.length - 2);
}
const properties = ["x", "y", "width", "height", "top", "left", "right", "bottom"];
2019-12-16 19:27:28 +01:00
async function performTest(output, callback){
2019-11-28 01:26:35 +01:00
const rects = getElements().map(function(element){
return {
name: element.dataset.name,
data: callback(element)
};
});
const data = new Float64Array(rects.length * properties.length);
rects.forEach(function(rect, i){
properties.forEach(function(property, j){
data[i * properties.length + j] = rect.data[property];
2019-03-16 00:52:28 +01:00
});
2019-11-28 01:26:35 +01:00
});
2019-12-16 19:27:28 +01:00
const hash = await crypto.subtle.digest("SHA-256", data);
output.querySelector(".hash").textContent = byteArrayToHex(hash);
2019-11-28 01:26:35 +01:00
2019-12-16 19:27:28 +01:00
const dataNode = output.querySelector(".data");
2019-11-28 01:26:35 +01:00
dataNode.innerHTML = "<table><tr><th></th>" +
rects.map(function(rect){
return "<th>" + rect.name + "</th>";
}).join("") +
"</tr><tr><th>hash</th>" +
rects.map(function(rect){
return "<td class=\"rectHash\" data-name=\"" + rect.name + "\"></td>";
}).join("") +
"</tr>" +
properties.map(function(property){
return "<tr><th>" + property + "</th>" + rects.map(function(rect){
return "<td class=\"value\" title=\"" + rect.data[property] + "\">" +
formatNumber(rect.data[property]) +
"</td>";
}).join("") + "</tr>";
}).join("") +
"</table>";
2019-12-16 19:27:28 +01:00
rects.forEach(async function(rect){
2019-11-28 01:26:35 +01:00
const data = new Float64Array(properties.length);
properties.forEach(function(property, i){
data[i] = rect.data[property];
2018-09-05 08:21:22 +02:00
});
2019-12-16 19:27:28 +01:00
const hash = await crypto.subtle.digest("SHA-256", data);
dataNode.querySelector(
".rectHash[data-name=\"" + rect.name + "\"]"
).textContent = byteArrayToHex(hash);
2019-11-28 01:26:35 +01:00
});
}
function createTest(title, callback){
2018-09-05 08:21:22 +02:00
const output = template.cloneNode(true);
output.querySelector(".title").textContent = title;
2019-11-28 01:26:35 +01:00
output.querySelector(".refresh").addEventListener("click", function(){
performTest(output, callback);
});
output.querySelector(".performance").addEventListener("click", function(){
let count = 200;
let totalCount = 0;
let totalDuration = 0;
return function(){
let duration = 0;
let i = 0;
while (duration < 1000){
const start = Date.now();
for (; i < count; i += 1){
const rects = getElements().map(function(element){
return {
name: element.dataset.name,
data: callback(element)
};
});
const data = new Float64Array(rects.length * properties.length);
rects.forEach(function(rect, i){
properties.forEach(function(property, j){
data[i * properties.length + j] = rect.data[property];
});
});
}
duration += Date.now() - start;
if (duration < 1000){
count += Math.ceil(
count * (1000 - duration) / 1000
);
}
}
totalCount += count;
totalDuration += duration;
alert(
(totalCount / (totalDuration / 1000)).toFixed(2) + " tests/s\n" +
(totalDuration * 1000 / totalCount).toFixed(2) + " µs/test\n" +
"(" + totalCount + " samples)"
);
};
}());
2018-09-05 08:21:22 +02:00
container.appendChild(output);
2019-11-28 01:26:35 +01:00
performTest(output, callback);
2018-09-05 08:21:22 +02:00
}
iframe.addEventListener("load", function(){
createTest("Element.getClientRects", function(element){
2018-09-05 08:21:22 +02:00
return element.getClientRects()[0];
});
createTest("Element.getBoundingClientRect", function(element){
2018-09-05 08:21:22 +02:00
return element.getBoundingClientRect();
});
createTest("Range.getClientRects", function(element){
2019-12-16 19:27:28 +01:00
const range = document.createRange();
range.selectNode(element);
return range.getClientRects()[0];
});
createTest("Range.getBoundingClientRect", function(element){
2019-12-16 19:27:28 +01:00
const range = document.createRange();
range.selectNode(element);
return range.getBoundingClientRect();
});
2018-09-05 08:21:22 +02:00
});
}());