Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
multi-order consolidateCartItems
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser:
Chrome 135
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
Map
615180.6 Ops/sec
Map + stack
636828.4 Ops/sec
lodash
558868.9 Ops/sec
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var body = { items: [ { goodsCode: '004566', quantity: 1, options: [ { goodsCode: '011774', quantity: 1, options: [ { goodsCode: '004605', quantity: 1, options: [ { goodsCode: '004606', quantity: 1, options: [], }, ], }, ], }, ], }, { goodsCode: '004566', quantity: 1, options: [ { goodsCode: '011774', quantity: 1, options: [ { goodsCode: '004605', quantity: 1, options: [ { goodsCode: '004606', quantity: 1, options: [], }, ], }, ], }, ], }, { goodsCode: '004566', quantity: 1, options: [ { goodsCode: '011774', quantity: 1, options: [ { goodsCode: '004605', quantity: 1, options: [ { goodsCode: '004607', quantity: 1, options: [], }, ], }, ], }, ], }, ], }
Tests:
Map
function consolidateCartItems(items) { // 동일한 옵션 구조로 그룹핑하기 위한 키 생성 함수 const shapeKey = (item) => { // 옵션이 없는 경우 goodsCode만으로 키 생성 if (!item.options || item.options.length === 0) { return item.goodsCode; } // 옵션이 있는 경우, 자식 옵션들의 키를 재귀적으로 생성 후 조합 const childKeys = item.options.map(shapeKey); return `${item.goodsCode}[${childKeys.join(',')}]`; }; // 그룹 정보 저장용 Map: key=shapeKey, value=CartItemDto const optionGroups = new Map(); // 입력된 각 아이템 순회 for (const item of items) { // 현재 항목의 옵션 구조 키 계산 const key = shapeKey(item); // 이미 옵션 그룹에 존재하는지 확인 const optionGroup = optionGroups.get(key); if (!optionGroup) { // 옵션 그룹이 없다면 새 그룹으로 초기화 optionGroups.set(key, { ...item, options: item.options ? [...item.options] : [], // 자식 옵션 복사 }); } else { // 옵션 그룹이 이미 존재한다면 수량 누적 optionGroup.quantity += item.quantity; // 옵션이 있으면 자식 배열에 추가 if (item.options) { optionGroup.options.push(...item.options); } } } const result = []; // Map에 모인 옵션 그룹 정보를 기반으로 CartItemDto 생성 for (const optionGroup of optionGroups.values()) { const consolidated = { goodsCode: optionGroup.goodsCode, // 대표 goodsCode 설정 quantity: optionGroup.quantity, // 누적된 수량 }; // 자식 옵션이 있다면 재귀적으로 병합하여 옵션 필드 설정 if (optionGroup.options.length > 0) { consolidated.options = consolidateCartItems(optionGroup.options); } result.push(consolidated); } // 최종 병합된 결과 반환 return result; } consolidateCartItems(body.items)
Map + stack
function consolidateCartItems(items) { // ─── 1) 동일 옵션 구조 판별용 키 생성 함수 ─── // 완전한 N차 구조를 문자열로 표현합니다. const shapeKey = (item) => { if (!item.options || item.options.length === 0) { return item.goodsCode; } // 자식 옵션들의 키를 재귀 생성 const childKeys = item.options.map(shapeKey); return `${item.goodsCode}[${childKeys.join(',')}]`; }; // ─── 2) 스택 초기화 ─── // rootOutput에 최종 결과가 쌓입니다. const rootOutput = []; const stack = [{ items, outputArr: rootOutput }]; // ─── 3) 스택 반복 처리 ─── while (stack.length > 0) { const { items: currItems, outputArr } = stack.pop(); // 현재 레벨에서 키별 그룹화(Map) → {goodsCode, quantity, children[]} const groups = new Map(); for (const item of currItems) { const key = shapeKey(item); const group = groups.get(key); if (!group) { // 신규 그룹 생성 groups.set(key, { goodsCode: item.goodsCode, quantity: item.quantity, // 하위 옵션을 모아둘 배열 children: item.options ? [...item.options] : [], }); } else { // 기존 그룹에 수량 누적, 옵션 병합 group.quantity += item.quantity; if (item.options) { group.children.push(...item.options); } } } // 그룹별로 DTO 생성, 옵션이 있으면 스택에 다시 푸시 for (const { goodsCode, quantity, children } of groups.values()) { const dto = { goodsCode, quantity }; outputArr.push(dto); if (children.length > 0) { // 이 dto의 options가 채워질 배열을 미리 생성 dto.options = []; // 스택에 넣어 하위 옵션 레벨을 같은 방식으로 처리 stack.push({ items: children, outputArr: dto.options }); } } } // rootOutput이 최종 결과 return rootOutput; } consolidateCartItems(body.items)
lodash
function consolidateCartItems(items) { // 1) 동일 옵션 구조 판별 키 생성 (재귀) const shapeKey = (item) => { if (!item.options || item.options.length === 0) { return item.goodsCode; } const childKeys = item.options.map(shapeKey); return `${item.goodsCode}[${childKeys.join(',')}]`; }; // 2) lodash.groupBy 로 같은 구조끼리 묶기 const groups = _.groupBy(items, shapeKey); // 3) 각 그룹별로 DTO 생성 및 하위 옵션(flatten) 재귀 처리 return Object.values(groups).map((groupItems) => { // 공통 goodsCode const goodsCode = groupItems[0].goodsCode; // 그룹 전체 수량 합산 const quantity = _.sumBy(groupItems, 'quantity'); // 모든 자식 옵션을 하나의 배열로 모음 const allOptions = _.flatten(groupItems.map((i) => i.options ?? [])); const consolidated = { goodsCode, quantity }; if (allOptions.length > 0) { // 자식 옵션이 있으면 재귀 호출 consolidated.options = consolidateCartItems(allOptions); } return consolidated; }); } consolidateCartItems(body.items)