mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-01-14 00:27:55 +00:00
Docs: Update benchmark results
This commit is contained in:
@@ -1,14 +1,10 @@
|
||||
const processCSV = async (csvString, config) => {
|
||||
const { parse } = await import('https://cdn.jsdelivr.net/npm/papaparse@5.4.1/+esm');
|
||||
async function processCSV(csvString, config) {
|
||||
const { parse } = await import('https://cdn.skypack.dev/papaparse@5.4.1');
|
||||
|
||||
const { data } = parse(csvString, {
|
||||
header: true,
|
||||
skipEmptyLines: true,
|
||||
dynamicTyping: true
|
||||
});
|
||||
const { data } = parse(csvString, { header: true, skipEmptyLines: true });
|
||||
|
||||
const filtered = data.filter(row =>
|
||||
row[config.filterColumn] == config.filterValue
|
||||
String(row[config.filterColumn]) === String(config.filterValue)
|
||||
);
|
||||
|
||||
const grouped = filtered.reduce((acc, row) => {
|
||||
@@ -18,15 +14,19 @@ const processCSV = async (csvString, config) => {
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
const operations = {
|
||||
sum: arr => arr.reduce((s, r) => s + (Number(r[config.aggregateColumn]) || 0), 0),
|
||||
avg: arr => operations.sum(arr) / arr.length || 0,
|
||||
count: arr => arr.length
|
||||
};
|
||||
|
||||
return Object.entries(grouped).map(([key, rows]) => ({
|
||||
[config.groupBy]: key,
|
||||
result: operations[config.operation](rows)
|
||||
}));
|
||||
};
|
||||
return Object.entries(grouped).map(([key, rows]) => {
|
||||
let result;
|
||||
const vals = rows.map(r => parseFloat(r[config.aggregateColumn]) || 0);
|
||||
|
||||
if (config.operation === 'sum') {
|
||||
result = vals.reduce((a, b) => a + b, 0);
|
||||
} else if (config.operation === 'avg') {
|
||||
result = vals.reduce((a, b) => a + b, 0) / vals.length;
|
||||
} else {
|
||||
result = rows.length;
|
||||
}
|
||||
|
||||
return { [config.groupBy]: key, result };
|
||||
});
|
||||
}
|
||||
export default processCSV;
|
||||
@@ -1,4 +1,4 @@
|
||||
const processCSV = async (csvString, config) => {
|
||||
async function processCSV(csvString, config) {
|
||||
const {
|
||||
filterColumn,
|
||||
filterValue,
|
||||
@@ -7,32 +7,38 @@ const processCSV = async (csvString, config) => {
|
||||
operation,
|
||||
} = config;
|
||||
|
||||
const [Papa, _] = 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 { default: Papa } = await import('https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.4.1/papaparse.min.js');
|
||||
|
||||
const { data } = Papa.parse(csvString, {
|
||||
header: true,
|
||||
dynamicTyping: true,
|
||||
skipEmptyLines: true,
|
||||
});
|
||||
|
||||
const aggregators = {
|
||||
sum: items => _.sumBy(items, aggregateColumn),
|
||||
count: items => items.length,
|
||||
avg: items => _.sumBy(items, aggregateColumn) / items.length || 0,
|
||||
sum: (acc, val) => (acc || 0) + val,
|
||||
count: (acc, _val) => (acc || 0) + 1,
|
||||
avg: (acc, val) => {
|
||||
const state = acc || { sum: 0, count: 0 };
|
||||
state.sum += val;
|
||||
state.count += 1;
|
||||
return state;
|
||||
},
|
||||
};
|
||||
|
||||
const { data } = await new Promise(resolve =>
|
||||
Papa.parse(csvString, {
|
||||
header: true,
|
||||
dynamicTyping: true,
|
||||
skipEmptyLines: true,
|
||||
complete: resolve,
|
||||
})
|
||||
);
|
||||
const groups = data
|
||||
.filter(row => row[filterColumn] == filterValue)
|
||||
.reduce((acc, row) => {
|
||||
const key = row[groupBy];
|
||||
acc[key] = aggregators[operation](acc[key], row[aggregateColumn]);
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
const filteredData = data.filter(row => row[filterColumn] === filterValue);
|
||||
const groupedData = _.groupBy(filteredData, groupBy);
|
||||
|
||||
return Object.entries(groupedData).map(([key, group]) => ({
|
||||
[groupBy]: isNaN(Number(key)) ? key : Number(key),
|
||||
result: aggregators[operation](group),
|
||||
return Object.entries(groups).map(([key, value]) => ({
|
||||
[groupBy]: key,
|
||||
result: operation === 'avg'
|
||||
? (value.count ? value.sum / value.count : 0)
|
||||
: value,
|
||||
}));
|
||||
};
|
||||
}
|
||||
export default processCSV;
|
||||
@@ -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