Last active
June 14, 2022 01:32
-
-
Save myfonj/57a6a8fcb1c5686527412543a897c918 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function parseRace(config){ | |
| const {snippets, _, itemCount, rounds, wrap} = config; | |
| console.table({ | |
| _: _.toString(), | |
| itemCount, | |
| rounds, | |
| wrap: wrap.toString() | |
| }) | |
| const results = {}; | |
| for (const snippet of snippets) { | |
| results[snippet] = [] | |
| } | |
| let i = rounds; | |
| const domparser = new DOMParser(); | |
| while(i-->0){ | |
| for ( const snippet of snippets ) { | |
| const contents = Array(itemCount).fill('').map( | |
| ()=>snippet.replace(/_/,_()) | |
| ).join(''); | |
| const html = wrap(contents); | |
| const start = performance.now(); | |
| const doc = domparser.parseFromString(html, 'text/html'); | |
| const duration = performance.now() - start; | |
| if(itemCount == 1 && rounds == 1){ | |
| console.info({ | |
| start, | |
| duration, | |
| "all.length":doc.all.length, | |
| html, | |
| outerHTML: doc.documentElement.outerHTML | |
| }); | |
| } | |
| results[snippet].push(duration) | |
| } | |
| } | |
| const tadah = Object.entries(results).map(([snippet,times])=>{ | |
| const sum = times.reduce((acc,v)=>acc+v); | |
| const median = computeMedian(times); | |
| return { | |
| snippet: snippet.replace(/\n/g,'\\n'), | |
| sum, | |
| median, | |
| average: sum / times.length, | |
| } | |
| }); | |
| console.table(tadah); | |
| } | |
| function compareNumbers(n1, n2) { | |
| n1 = Number(n1); | |
| n2 = Number(n2); | |
| if( n1 < n2 ) return -1 | |
| if( n1 === n2 ) return 0; | |
| if( n1 > n2 ) return 1 | |
| if(Number.isNaN(n1) || Number.isNaN(n2)) { | |
| throw Error("Sorting NaNs, seriously?") | |
| } else { | |
| throw Error("WAT?!") | |
| } | |
| } | |
| function computeMedian(arrr) { | |
| const l = arrr.length; | |
| const arr = [].slice.call(arrr).sort(compareNumbers); | |
| if(l === 1) { | |
| return arr[0] | |
| } | |
| if(l === 2) { | |
| return (arr[0] + arr[1]) / 2 | |
| } | |
| const mid = l / 2; | |
| if(l % 2 == 0) { | |
| return computeMedian([ | |
| arr[mid], | |
| arr[mid-1], | |
| ]); | |
| } else { | |
| return arr[Math.floor(mid)] | |
| } | |
| } | |
| parseRace({ | |
| snippets: [ | |
| `<p>_`, | |
| `<p>_</p>`, | |
| `<p>_abcd`, | |
| `<p>_\n`, | |
| `<p>_</p>\n`, | |
| `<p>_abcd\n`, | |
| `<p>_\n</p>\n`, | |
| `<p>_</p>\n\n`, | |
| `<p>_abcd\n\n`, | |
| `<p>_\n\n` | |
| ], | |
| _: ()=>Array(4).fill('').map(()=>String(Math.random())).join('').replace(/[.0]+/g,' ').trim(), | |
| itemCount: 500, | |
| rounds: 100, | |
| wrap: (_)=>`<!doctype html>${_}` | |
| }); | |
| // https://news.ycombinator.com/item?id=31697790 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment