Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fight to the death - for loop vs chained arr (100)
(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 < 100; 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]
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):
Measuring the performance of JavaScript loops is crucial in understanding how different approaches can impact application performance. The benchmark provided tests two approaches to sorting an array of objects based on their start date: a traditional for loop and a chained array method. **For Loop** In this approach, a for loop is used to iterate over the `workArr` array. Inside the loop, each object is compared with the current date using a switch statement. If the object's start date is less than or equal to the current date, it is added to the `pastArr`. If the object's start date is greater than the current date and `futureArr` is empty, it is assigned to `futureArr`. If the object's start date is between the current date and the first object in `futureArr`, `futureArr` is updated with the new object. Finally, both arrays are returned as an array of arrays. Pros: * Easy to understand and implement * Can be optimized for performance by using techniques like early returns and caching Cons: * Can be slow due to the use of a switch statement inside a loop * May require additional optimization steps, such as memoizing the comparison function **Chained Array Method** In this approach, a chained array method is used to filter and sort the `workArr` array. The filtering process uses the `filter()` method to create a new array with objects whose start date is less than or equal to the current date. The sorting process uses the `sort()` method to sort the filtered array based on the start date. Finally, the sorted array is returned. Pros: * Can be faster due to the use of optimized filtering and sorting algorithms * Can take advantage of caching and memoization Cons: * Can be less intuitive for developers without experience with chained array methods * May require additional optimization steps, such as using a more efficient data structure or caching intermediate results **Library Used: None** There are no libraries used in this benchmark, making it a vanilla JavaScript implementation. **Special JS Features/Syntax: None** There are no special JavaScript features or syntax used in this benchmark. It's a straightforward implementation of two common loop approaches. **Alternative Approaches:** 1. **Using `Array.prototype.forEach()`**: Instead of using a for loop, you can use the `forEach()` method to iterate over the array and compare each object with the current date. 2. **Using `Array.prototype.reduce()`**: You can use the `reduce()` method to accumulate the filtered objects into an array. 3. **Using a more efficient data structure**: Instead of using an array, you could use a data structure like a binary search tree or a heap to take advantage of faster lookup and insertion times. 4. **Caching intermediate results**: You can cache intermediate results, such as the current date and the filtered objects, to reduce the number of computations required. Overall, both approaches have their pros and cons, and the choice of which one to use depends on the specific requirements of your application and your team's expertise.
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 methods1 array length 100
fight to the death - for loop vs chained arr (10)
Comments
Confirm delete:
Do you really want to delete benchmark?