mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-01-14 08:37:56 +00:00
Docs: Update benchmark results
This commit is contained in:
@@ -1,32 +1,24 @@
|
||||
async function processCSV(csvString, config) {
|
||||
const { parse } = await import('https://cdn.skypack.dev/papaparse@5.4.1');
|
||||
const processCSV = async (csv, { filterColumn, filterValue, groupBy, aggregateColumn, operation }) => {
|
||||
const { parse } = await import('https://cdn.skypack.dev/csv-parse/sync');
|
||||
|
||||
const { data } = parse(csvString, { header: true, skipEmptyLines: true });
|
||||
const records = parse(csv, { columns: true, skip_empty_lines: true });
|
||||
|
||||
const filtered = data.filter(row =>
|
||||
String(row[config.filterColumn]) === String(config.filterValue)
|
||||
);
|
||||
const filtered = records.filter(r => r[filterColumn] == filterValue);
|
||||
|
||||
const grouped = filtered.reduce((acc, row) => {
|
||||
const key = row[config.groupBy];
|
||||
if (!acc[key]) acc[key] = [];
|
||||
acc[key].push(row);
|
||||
const groups = filtered.reduce((acc, row) => {
|
||||
const key = row[groupBy];
|
||||
(acc[key] ??= []).push(row);
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
return Object.entries(grouped).map(([key, rows]) => {
|
||||
let result;
|
||||
const vals = rows.map(r => parseFloat(r[config.aggregateColumn]) || 0);
|
||||
return Object.entries(groups).map(([key, rows]) => {
|
||||
const vals = rows.map(r => Number(r[aggregateColumn]) || 0);
|
||||
const sum = vals.reduce((a, b) => a + b, 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 };
|
||||
return {
|
||||
[groupBy]: key,
|
||||
result: operation === 'sum' ? sum : operation === 'avg' ? sum / vals.length : vals.length
|
||||
};
|
||||
});
|
||||
}
|
||||
};
|
||||
export default processCSV;
|
||||
@@ -1,44 +1,39 @@
|
||||
async function processCSV(csvString, config) {
|
||||
const {
|
||||
filterColumn,
|
||||
filterValue,
|
||||
groupBy,
|
||||
aggregateColumn,
|
||||
operation,
|
||||
} = config;
|
||||
async function processCSV(csv, {
|
||||
filterColumn,
|
||||
filterValue,
|
||||
groupBy,
|
||||
aggregateColumn,
|
||||
operation,
|
||||
}) {
|
||||
const { default: Papa } = await import(
|
||||
'https://cdn.jsdelivr.net/npm/papaparse@5.4.1/papaparse.min.js'
|
||||
);
|
||||
|
||||
const { default: Papa } = await import('https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.4.1/papaparse.min.js');
|
||||
const { data } = Papa.parse(csv, { header: true, skipEmptyLines: true });
|
||||
|
||||
const { data } = Papa.parse(csvString, {
|
||||
header: true,
|
||||
dynamicTyping: true,
|
||||
skipEmptyLines: true,
|
||||
});
|
||||
const groups = data.reduce((acc, row) => {
|
||||
if (row[filterColumn] != filterValue) {
|
||||
return acc;
|
||||
}
|
||||
|
||||
const key = row[groupBy];
|
||||
const stats = acc.get(key) || { sum: 0, count: 0 };
|
||||
|
||||
stats.sum += Number(row[aggregateColumn]) || 0;
|
||||
stats.count++;
|
||||
|
||||
return acc.set(key, stats);
|
||||
}, new Map());
|
||||
|
||||
const aggregators = {
|
||||
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;
|
||||
},
|
||||
sum: ({ sum }) => sum,
|
||||
avg: ({ sum, count }) => (count ? sum / count : 0),
|
||||
count: ({ count }) => count,
|
||||
};
|
||||
|
||||
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;
|
||||
}, {});
|
||||
|
||||
return Object.entries(groups).map(([key, value]) => ({
|
||||
return Array.from(groups, ([key, stats]) => ({
|
||||
[groupBy]: key,
|
||||
result: operation === 'avg'
|
||||
? (value.count ? value.sum / value.count : 0)
|
||||
: value,
|
||||
result: aggregators[operation](stats),
|
||||
}));
|
||||
}
|
||||
export default processCSV;
|
||||
22
tests/6_csv_processor/outputs/openai_gpt-5.1-chat.js
Normal file
22
tests/6_csv_processor/outputs/openai_gpt-5.1-chat.js
Normal file
@@ -0,0 +1,22 @@
|
||||
async function processCSV(csv, cfg) {
|
||||
const p = await import("https://cdn.jsdelivr.net/npm/papaparse@5.4.1/papaparse.min.js");
|
||||
const f = p.parse(csv, { header: true }).data.filter(r => r[cfg.filterColumn] == cfg.filterValue);
|
||||
const g = {};
|
||||
for (const r of f) {
|
||||
const k = r[cfg.groupBy];
|
||||
if (!g[k]) g[k] = [];
|
||||
g[k].push(r);
|
||||
}
|
||||
const out = [];
|
||||
for (const k in g) {
|
||||
const rows = g[k];
|
||||
let nums = rows.map(r => +r[cfg.aggregateColumn] || 0);
|
||||
let res = 0;
|
||||
if (cfg.operation === "sum") res = nums.reduce((a, b) => a + b, 0);
|
||||
else if (cfg.operation === "avg") res = nums.reduce((a, b) => a + b, 0) / nums.length;
|
||||
else if (cfg.operation === "count") res = rows.length;
|
||||
out.push({ [cfg.groupBy]: k, result: res });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
export default processCSV;
|
||||
@@ -1,21 +1,16 @@
|
||||
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)}));
|
||||
async function processCSV(csv,cfg){
|
||||
const [{Papa},{groupBy:gb}]=await Promise.all([
|
||||
import('https://cdn.jsdelivr.net/npm/papaparse@5.4.1/papaparse.min.mjs'),
|
||||
import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.js')
|
||||
])
|
||||
const {filterColumn:fc,filterValue:fv,groupBy:gbk,aggregateColumn:ac,operation:op}=cfg
|
||||
const rows=Papa.parse(csv,{header:!0,skipEmptyLines:!0}).data.filter(r=>r&&r[fc]==fv)
|
||||
const grouped=gb(rows,r=>r?.[gbk]??'')
|
||||
return Object.entries(grouped).map(([k,arr])=>{
|
||||
const nums=arr.map(r=>{const n=+r[ac];return Number.isFinite(n)?n:0})
|
||||
const sum=nums.reduce((a,b)=>a+b,0)
|
||||
const result=op==='sum'?sum:op==='avg'?sum/nums.length:arr.length
|
||||
return {[gbk]:k,result}
|
||||
})
|
||||
}
|
||||
export default processCSV;
|
||||
Reference in New Issue
Block a user