start repo with what exists

This commit is contained in:
Alexander Mahr 2024-11-03 08:36:19 +01:00
commit b4bf1f9a9f
5 changed files with 177 additions and 0 deletions

5
README.md Normal file
View file

@ -0,0 +1,5 @@
# some HTML+JS files (to run some tests)
## why not node ?
a) because some should reflect browser behavior rather than node
b) why not?

7
test1.html Normal file
View file

@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<style>
</style>
<h1> test1.js <h1>
<script src="test1.js"></script>
</html>

59
test1.js Normal file
View file

@ -0,0 +1,59 @@
window.addEventListener("load",async ()=> {
var info = document.createElement("div");
info.innerHTML="both <br> test A: (new TextEncoder()).encode(str) and <br> test B: new Blob([str])<br> will block the UI thread equally<br><br>"
info.style.color="blue";
document.body.appendChild(info);
var div = document.createElement("div");
var acount=0;
var bcount=0;
var end;
var secs=10;
var done=false
document.body.appendChild(div);
(function info(){
div.innerHTML="end in "+(end - Date.now())
+"<br>acount="+acount
+"<br>bcount="+bcount;
if(!done)
setTimeout(info,100);
})();
function wait(ms=100){
return new Promise((resolve)=>{
setTimeout(resolve,ms);
});
}
await (()=>{return new Promise((resolve)=>{console.log('start promise');setTimeout(resolve,100);});})();
var a = (new Array(20000000)).join(".");
const enc = new TextEncoder();
var ab;
console.log("start test a");
end = Date.now() + secs * 1000;
while(Date.now()<end){
acount++;
for(let i=0; i<10; i++)
{
ab = enc.encode(a);
await wait(0);
var first = new Uint8Array(ab)[0];
}
}
console.log("ended test a",acount);
console.log("start test b");
end = Date.now() + secs * 1000;
while(Date.now()<end){
bcount++;
for(let i=0; i<10; i++)
{
//ab = await new Blob([a]).arrayBuffer();
new Blob([a]);
}
}
console.log("ended test b",bcount);
done=true;
},false);

9
test2.html Normal file
View file

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<style>
</style>
<h1> test2.js <h1>
<h2> worst case gzip </h2>
<p> trying to see if the case to do gzip => ungzip is detrimental in terms of size increase for random data </p>
<script src="test2.js"></script>
</html>

97
test2.js Normal file
View file

@ -0,0 +1,97 @@
window.addEventListener("load",async ()=> {
async function nnDecompress(blob) {
const ds = new DecompressionStream("gzip");
const decompressedStream = blob.stream().pipeThrough(ds);
return await (await new Response(decompressedStream).blob()).text();
}
async function nnCompress(blob) {
const cs = new CompressionStream("gzip");
const compressedStream = blob.stream().pipeThrough(cs);
return await new Response(compressedStream).blob();
}
var info = document.createElement("div");
info.innerHTML="both <br> test A: (new TextEncoder()).encode(str) and <br> test B: new Blob([str])<br> will block the UI thread equally<br><br>"
info.style.color="blue";
document.body.appendChild(info);
var textarea = document.createElement("textarea");
textarea.textContent="";
textarea.style.width="90vw";
textarea.style.height="100vh";
document.body.appendChild(textarea);
for(var i=0; i<=23; i++)
{
let bytes = 1 << i;
let array = new Uint8Array(bytes);
let zerozipped = await nnCompress(new Blob([array]));
crypto.getRandomValues(array);
let zipped = await nnCompress(new Blob([array]));
textarea.textContent+="\n"+i+"\n"+"with "+bytes+" bytes"+"\n"+
"===> "+zipped.size+" compression ("+(((zipped.size/bytes*10000)|0)/100)+"%)\n"+
"===> "+zerozipped.size+" compression ("+(((zerozipped.size/bytes*10000)|0)/100)+"%)\n"+
Array.from(new Uint8Array(await zipped.slice(0,32).arrayBuffer())).map((e)=>{ return ("0"+e.toString(16)).slice(-2);}).join(" ")+"\n";
Array.from(new Uint8Array(await zerozipped.slice(0,32).arrayBuffer())).map((e)=>{ return ("0"+e.toString(16)).slice(-2);}).join(" ")+"\n";
// var gzippeduint = new Uint8Array(await gzipped.arrayBuffer());
}
// var div = document.createElement("div");
// var acount=0;
// var bcount=0;
// var end;
// var secs=10;
// var done=false
// document.body.appendChild(div);
// (function info(){
// div.innerHTML="end in "+(end - Date.now())
// +"<br>acount="+acount
// +"<br>bcount="+bcount;
// if(!done)
// setTimeout(info,100);
// })();
//
// function wait(ms=100){
// return new Promise((resolve)=>{
// setTimeout(resolve,ms);
// });
// }
//
// await (()=>{return new Promise((resolve)=>{console.log('start promise');setTimeout(resolve,100);});})();
// var a = (new Array(20000000)).join(".");
// const enc = new TextEncoder();
// var ab;
//
// console.log("start test a");
// end = Date.now() + secs * 1000;
// while(Date.now()<end){
// acount++;
// for(let i=0; i<10; i++)
// {
// ab = enc.encode(a);
// await wait(0);
// var first = new Uint8Array(ab)[0];
// }
// }
// console.log("ended test a",acount);
//
// console.log("start test b");
// end = Date.now() + secs * 1000;
// while(Date.now()<end){
// bcount++;
// for(let i=0; i<10; i++)
// {
// //ab = await new Blob([a]).arrayBuffer();
// new Blob([a]);
// }
// }
// console.log("ended test b",bcount);
// done=true;
},false);