mirror of
https://github.com/lynchmarkHQ/lynchmark.com.git
synced 2026-10-16 08:57:52 +00:00
Docs: Update benchmark results
This commit is contained in:
@@ -1,16 +1,21 @@
|
||||
export const processCSV=async(csv,cfg)=>{
|
||||
const [{parse},{groupBy,sumBy}]=await Promise.all([
|
||||
import('https://cdn.jsdelivr.net/npm/papaparse@5.4.1/+esm'),
|
||||
import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/+esm')
|
||||
]);
|
||||
const {filterColumn:fc,filterValue:fv,groupBy:gb,aggregateColumn:ac,operation:op}=cfg;
|
||||
const rows=parse(csv,{header:!0,skipEmptyLines:!0}).data.filter(r=>r[fc]==fv);
|
||||
const grouped=groupBy(rows,r=>r[gb]);
|
||||
const agg=v=>{
|
||||
if(op=='count')return v.length;
|
||||
const total=sumBy(v,x=>+x[ac]||0);
|
||||
return op=='avg'&&v.length?total/v.length:total;
|
||||
};
|
||||
return Object.entries(grouped).map(([k,v])=>({[gb]:k,result:agg(v)}));
|
||||
};
|
||||
const libs=(()=>{let c;return()=>c||(c=Promise.all([
|
||||
import('https://cdn.jsdelivr.net/npm/d3-dsv@3/+esm'),
|
||||
import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/groupBy.js')
|
||||
]).then(([dsv,gb])=>({parse:dsv.csvParse,groupBy:gb.default||gb})));})();
|
||||
|
||||
const toNum=v=>{const n=+v;return Number.isFinite(n)?n:0};
|
||||
|
||||
async function processCSV(csv,opts){
|
||||
const {parse,groupBy}=await libs();
|
||||
const rows=parse(csv);
|
||||
const {filterColumn:fc,filterValue:fv,groupBy:gc,aggregateColumn:ac,operation:op}=opts;
|
||||
const pool=groupBy(rows.filter(r=>r[fc]==fv),r=>r[gc]??'');
|
||||
const calc={
|
||||
sum:x=>x.reduce((t,r)=>t+toNum(r[ac]),0),
|
||||
avg:x=>x.length?x.reduce((t,r)=>t+toNum(r[ac]),0)/x.length:0,
|
||||
count:x=>x.length
|
||||
}[op];
|
||||
if(!calc) throw new Error('Unsupported operation');
|
||||
return Object.entries(pool).map(([k,x])=>({[gc]:k,result:calc(x)}));
|
||||
}
|
||||
export default processCSV;
|
||||
Reference in New Issue
Block a user