Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fight to the death - for loop vs chained methods 10,000
(version: 0)
Comparing performance of:
for loop vs chained arr
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const randomStDate = () => { const randD = new Date(new Date("2021-12-05").getTime() + Math.random() * (new Date("2026-12-05").getTime() - new Date("2023-12-05").getTime())) let month = '' + (randD.getMonth() + 1); let day = '' + randD.getDate(); let year = randD.getFullYear(); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; return [year, month, day].join('-'); }; const rndId = Math.floor(Math.random() * (100000000 - 1 + 1) + 1) const createArr = () => { let workArr = [] for (let i = 0; i < 10000; i++) { const pl = { tisId: rndId, startDate: randomStDate(), endDate: "2026-12-05", site: "Liverpool", } workArr.push(pl) } return workArr } var workArr = createArr() var today = new Date().toISOString().slice(0, 10)
Tests:
for loop
let pastArr = []; let futureArr = []; for (let i = 0; i < workArr.length; i++) { const pl = workArr[i]; switch(true){ case pl.startDate < today : pastArr.push(pl); break; case futureArr.length < 1 && pl.startDate >= today : futureArr = [pl]; break; case futureArr.length > 0 && pl.startDate < futureArr[0].startDate : futureArr = [pl]; break; case futureArr.length > 0 && pl.startDate === futureArr[0].startDate : futureArr.push(pl); break; } } return [...pastArr, ...futureArr].sort((a, b) => (a.startDate > b.startDate ? 1 : -1));
chained arr
const today = new Date().toISOString().slice(0,10) const firstFutureWorks = workArr .filter(w => w.startDate > today) .sort((a, b) => (a.startDate > b.startDate ? 1 : -1)); const nextFutureDate = firstFutureWorks[0] ? firstFutureWorks[0].startDate : today; return workArr.filter(w => w.startDate <= nextFutureDate);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for loop
chained arr
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):
**Benchmark Explanation** The provided JSON represents a JavaScript microbenchmark test case, which compares the performance of two approaches: a traditional `for` loop and a chained array method. The benchmark tests the sorting and filtering of an array containing 10,000 objects with dates. The objects are created using the `createArr` function, which generates an array of objects with random start dates between December 5, 2021, and December 5, 2026. **For Loop** The first test case uses a traditional `for` loop to iterate through the array and sort it based on the start date. The loop is used to: 1. Initialize two empty arrays: `pastArr` and `futureArr`. 2. Iterate through each object in the `workArr` array using an index variable (`i`). 3. Use a `switch` statement to determine which array to push the current object into: * If the start date is before the current date, push it into `pastArr`. * If there are no objects in `futureArr` or the start date is on or after the current date, create a new `futureArr` with the current object. * If the start date is between the dates of two existing objects in `futureArr`, replace the first object with the current one. 4. Return the sorted and filtered array by concatenating `pastArr` and `futureArr` and sorting it based on the start date. **Chained Array** The second test case uses a chained array method to achieve the same result: 1. Filter the `workArr` array to get only the objects with dates greater than or equal to the current date. 2. Sort the filtered array by start date in descending order. 3. If there is no object with a start date greater than the current date, use the current date as the next future date. 4. Return the filtered and sorted array. **Comparison** The benchmark compares the performance of these two approaches: * **For Loop**: Uses an explicit loop to iterate through the array, which can be slower due to the overhead of the loop variable and the `switch` statement. * **Chained Array**: Uses a more functional programming style, with methods like `filter()` and `sort()`, which are typically faster because they rely on optimized internal implementations. **Pros and Cons** **For Loop:** Pros: * Can be easier to understand and maintain for some developers. * Allows for more control over the iteration process. Cons: * Can be slower due to the explicit loop and conditional statements. **Chained Array:** Pros: * Typically faster because it uses optimized internal implementations of filtering and sorting methods. * More concise and expressive code. Cons: * May require more mental overhead to understand the code, especially for developers not familiar with functional programming. **Other Considerations** * **Use cases**: The `for` loop approach might be preferred when working with large datasets or when specific iteration requirements need to be met. The chained array method is often a better choice when dealing with smaller datasets or when the focus is on concise, expressive code. * **Browser support**: Both approaches are supported by modern browsers, but some older browsers may have issues with certain methods or syntax. **Alternative Approaches** Other alternatives for this benchmark could include: * Using `Array.prototype.reduce()` instead of a traditional loop. * Utilizing libraries like Lodash to simplify the filtering and sorting process. * Employing more advanced data structures, such as a binary search tree or a heap, for efficient sorting and filtering.
Related benchmarks:
fight to the death - for loop vs chained array methods (10)
fight to the death - for loop vs chained methods1
fight to the death - for loop vs chained methods 100,000
fight to the death - for loop vs chained methods 1,000,000
Comments
Confirm delete:
Do you really want to delete benchmark?