Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fight to the death - for loop vs chained methods 100,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 < 100000; 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 Overview** The provided benchmark compares two approaches for processing and sorting an array of objects in JavaScript: a traditional `for` loop with a `switch` statement, and a chained array method approach using `filter`, `sort`, and conditional statements. **For Loop Approach** * **Description**: The `for` loop iterates over the `workArr` array, comparing each object's `startDate` property with the current date (`today`) using a `switch` statement. Depending on the comparison result, the object is either added to the `pastArr`, `futureArr`, or updated in `futureArr`. * **Pros**: * Simple and intuitive implementation. * Easy to understand and maintain for developers familiar with `for` loops and `switch` statements. * **Cons**: * Inefficient due to the use of a `switch` statement, which can be slower than other comparison methods. * Limited flexibility in handling complex logic or edge cases. **Chained Array Method Approach** * **Description**: The chained array method approach uses `filter`, `sort`, and conditional statements to process the `workArr` array. It first filters out objects with a start date greater than today, then sorts the remaining objects by their start date. If there are multiple objects with the same start date, it updates the start date of each object in the sorted array. * **Pros**: * More efficient and concise compared to the `for` loop approach. * Easier to maintain and modify due to its modular nature (filtering, sorting, and updating). * **Cons**: * Requires a deeper understanding of JavaScript's array methods and their behavior. * May be less readable for developers unfamiliar with these methods. **Library Usage** The benchmark uses the `Math.random()` function from the built-in `Math` object to generate random numbers. This is not considered a library usage in this context, as it's a standard JavaScript function. **Special JS Features/Syntax** None of the provided code snippets use any special JavaScript features or syntax that would require additional explanation. **Alternatives** If you're looking for alternative approaches to process and sort arrays in JavaScript, consider: * Using `Array.prototype.map`, `Array.prototype.reduce`, or other array methods for more efficient processing. * Leveraging modern JavaScript libraries like Lodash or Ramda for functional programming and array manipulation. * Utilizing streaming algorithms or iterative methods for handling large datasets. Keep in mind that the choice of approach depends on the specific requirements, data size, and performance needs of your project.
Related benchmarks:
fight to the death - for loop vs chained methods1 array length 100
fight to the death - for loop vs chained methods 1000
fight to the death - for loop vs chained methods 10,000
fight to the death - for loop vs chained methods 1,000,000
Comments
Confirm delete:
Do you really want to delete benchmark?