Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Fix Lodash_4.17.21.sortBy vs .sort vs custom insertion vs custom quick
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser:
Chrome 120
Operating system:
Linux
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
Lodash_4.17.21.sortBy
93942.1 Ops/sec
stdSort
67450.7 Ops/sec
InsertionSort
50925.3 Ops/sec
QuickSort
44675.1 Ops/sec
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js'></script>
Script Preparation code:
var arr = [{ name: "Ваня", date: "2024-12-01 18:00", curr: "RUB", level: 12, }, { name: "Маня", date: "2024-12-01 02:00", curr: "KZT", level: 3, }, { name: "Петя", date: "2024-12-01 12:00", curr: "USD", level: 7, }, { name: "Вика", date: "2024-12-01 05:00", curr: "RUB", level: 9, }, { name: "Лера", date: "2024-12-01 11:00", curr: "RUB", level: 2, }, { name: "Каша", date: "2024-12-01 15:00", curr: "KZT", level: 10, }, { name: "Паша", date: "2024-12-01 17:00", curr: "USD", level: 1, }, { name: "Даша", date: "2024-12-01 10:00", curr: "RUB", level: 8, }, { name: "Вера", date: "2024-12-01 00:00", curr: "RUB", level: 5, }, { name: "Саша", date: "2024-12-01 14:00", curr: "USD", level: 11, }, { name: "Маша", date: "2024-12-01 10:00", curr: "KZT", level: 4, }, { name: "Ваня", date: "2024-12-01 15:00", curr: "USD", level: 6, }, ]; var CURR_ORDER = { RUB: 1, USD: 2, KZT: 3, }; var CONDITIONS = [ (a, b) => CURR_ORDER[a.curr] - CURR_ORDER[b.curr], (a, b) => new Date(a.date) - new Date(b.date), ]; var compare = (conditions) => (a, b) => { for (let i = 0; i < conditions.length; i++) { const condition = conditions[i]; const type = typeof condition; if (type === "string") { if (typeof a[condition] === "string") { const result = a[condition].localeCompare(b[condition]); if (!result) continue; return result; } else if (typeof a[condition] === "number") { const result = a[condition] - b[condition]; if (!result) continue; return result; } } else if (type === "function") { const result = condition(a, b); if (!result) continue; return result; } } return 0; }; var stdSort = (arr, conditions) => { const newArr = [...arr]; newArr.sort(compare(conditions)); return newArr; }; var insertionSort = (arr, conditions) => { const newArr = [...arr]; for (let i = 1, l = newArr.length; i < l; i++) { const current = arr[i]; let j = i; const comp = compare(conditions); while (j > 0 && comp(newArr[j - 1], current)) { newArr[j] = newArr[j - 1]; j--; } newArr[j] = current; } return newArr; }; var partition = (arr, start, end, compare) => { const pivotValue = arr[end]; let pivotIndex = start; for (let i = start; i < end; i++) { if (compare(arr[i], pivotValue) < 0) { [arr[i], arr[pivotIndex]] = [arr[pivotIndex], arr[i]]; pivotIndex++; } } [arr[pivotIndex], arr[end]] = [arr[end], arr[pivotIndex]]; return pivotIndex; }; var quickSortIterative = (arr, conditions) => { const newArr = [...arr]; const stack = [0, arr.length - 1]; const comp = compare(conditions); while (stack[stack.length - 1] >= 0) { end = stack.pop(); start = stack.pop(); pivotIndex = partition(newArr, start, end, comp); if (pivotIndex - 1 > start) { stack.push(start); stack.push(pivotIndex - 1); } if (pivotIndex + 1 < end) { stack.push(pivotIndex + 1); stack.push(end); } } return newArr; };
Tests:
Lodash_4.17.21.sortBy
_.sortBy(arr, [(o) => CURR_ORDER[o.curr], (o) => new Date(o.date)]);
stdSort
stdSort(arr, CONDITIONS);
InsertionSort
insertionSort(arr, CONDITIONS);
QuickSort
quickSortIterative(arr, CONDITIONS);