Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test task
(version: 0)
Comparing performance of:
1reduce1foreach vs Arrays vs 2reduce
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.content = [{ "date": "2017-07-31", "amount": "5422" }, { "date": "2017-06-30", "amount": "5220" }, { "date": "2017-05-31", "amount": "5365" }, { "date": "2017-08-31", "amount": "5451" }, { "date": "2017-09-30", "amount": "5303" }, { "date": "2018-03-31", "amount": "5654" }, { "date": "2017-10-31", "amount": "5509" }, { "date": "2017-12-31", "amount": "5567" }, { "date": "2018-01-31", "amount": "5597" }, { "date": "2017-11-30", "amount": "5359" }, { "date": "2018-02-28", "amount": "5082" }, { "date": "2018-04-14", "amount": "2567" } ];
Tests:
1reduce1foreach
function sortOperations(operations) { const withoutSorting = operations.reduce((acc, {date}) => { const [year, month, day] = date.split('-') const monthDay=`${month}-${day}` return { ...acc, [year]: Array.isArray(acc[year]) ? [...acc[year], monthDay] : [monthDay]} }, {}) Object.entries(withoutSorting).forEach(([key, values])=>{withoutSorting[key] = values.sort()}) return withoutSorting } sortOperations(window.content)
Arrays
function sortOperations(operations){ let splittedDates = operations.map((e)=>e.date.split("-")) let years = Array.from(new Set(splittedDates.map((e)=>e[0]))) let output={} years.forEach((e)=>{ output[e] = splittedDates.filter(e1=>e1[0]===e).map(e1=>[e1[1],e1[2]].join("-")).sort() }) return output } sortOperations(window.content)
2reduce
function sortOperations(operations) { const withoutSorting = operations.reduce((acc, {date}) => { const [year, month, day] = date.split('-') const monthDay=`${month}-${day}` return { ...acc, [year]: Array.isArray(acc[year]) ? [...acc[year], monthDay] : [monthDay]} }, {}) return Object.entries(withoutSorting).reduce((acc, [key, values])=> ({ ...acc, [key]: values.sort((a,b) => +a.replace(/-/g, '') - +b.replace(/-/g, '')) }), {}) } sortOperations(window.content)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
1reduce1foreach
Arrays
2reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of JavaScript benchmarks can be fascinating. The provided JSON represents a benchmark definition and test cases for measuring the performance of sorting operations on arrays. There are three individual test cases: 1. `sortOperations(operations) without using reduce()`: This function creates an object where each key is a year, and its corresponding value is an array of month-day pairs in sorted order. Pros: * Simple implementation * Easy to understand Cons: * Using `forEach` loop for sorting can be inefficient * Requires extra memory allocation for the output object 2. `sortOperations(operations) using Arrays`: This function uses the `map`, `Set`, and `filter` methods to group dates by year, sort them, and then create an object with year as key and sorted month-day pairs as value. Pros: * More efficient than the first approach (using `forEach`) * Uses built-in JavaScript methods, making it more readable Cons: * More complex implementation * Requires understanding of array manipulation methods 3. `sortOperations(operations) using reduce()`: This function creates an object where each key is a year, and its corresponding value is an array of month-day pairs in sorted order. Pros: * Using `reduce()` method for aggregation can be efficient * Easy to understand Cons: * Requires extra memory allocation for the output object * Can be slower than using `forEach` loop for sorting Regarding libraries or special JavaScript features used in these test cases: * None of the provided code uses any external libraries. * The tests use built-in JavaScript methods like `map`, `Set`, `filter`, and `reduce()`. * No special JavaScript features are used (e.g., ES6 syntax, async/await, Promises). Other alternatives for measuring performance benchmarks could include: * Using a different data structure, such as a trie or a heap, to sort the dates * Implementing the sorting logic using a custom algorithm * Using a different programming language or framework * Creating multiple test cases with varying input sizes and complexity * Integrating the benchmark with other tools or frameworks for automated testing Keep in mind that the choice of approach depends on the specific requirements and goals of the benchmark.
Related benchmarks:
Loop perf
Array.map vs reduce
dsdsdsdsdsdsds
asdfasdfasdf
ISO 8601 parsing
Comments
Confirm delete:
Do you really want to delete benchmark?