Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fight to the death - for loop vs chained (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 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):
I'll break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The test compares two JavaScript approaches to filter an array of objects based on a date condition: 1. **For Loop Approach**: Uses a traditional `for` loop to iterate through the array and make decisions about which objects to keep or discard. 2. **Chained Array Approach**: Utilizes the `filter()` and `sort()` methods in combination with conditional statements to achieve the same filtering result. **What's Being Tested** The benchmark aims to determine which approach is faster for a specific use case: * Create an array of 100 objects with random dates (`startDate` and `endDate`) between December 5, 2021, and December 5, 2026. * Filter the array to include only objects where the `startDate` is on or before today's date. **Options Compared** The two approaches being tested are: A) **For Loop Approach** B) **Chained Array Approach** **Pros and Cons of Each Approach:** ### For Loop Approach Pros: 1. **Explicit iteration**: The `for` loop allows for explicit control over the iteration process, making it easier to understand and modify. 2. **No reliance on array methods**: This approach doesn't rely on external array methods, which can be beneficial in certain situations. Cons: 1. **Performance overhead**: Iterating through an array using a `for` loop can be slower than utilizing optimized array methods like `filter()` and `sort()`. 2. **Code verbosity**: The `for` loop approach can result in more verbose code compared to the chained array method. ### Chained Array Approach Pros: 1. **Performance optimization**: Utilizing optimized array methods like `filter()` and `sort()` can lead to better performance. 2. **Concise code**: The chained array approach typically results in shorter, more concise code. Cons: 1. **Reliance on external methods**: This approach relies on external methods (e.g., `filter()`, `sort()`) that might not be suitable for all situations or are less intuitive. 2. **Potential readability issues**: The use of multiple conditional statements and array methods can make the code harder to read and understand. **Library Used** The benchmark does not explicitly mention any libraries being used, but it's essential to consider that some modern JavaScript engines may rely on internal optimizations or polyfills for these array methods. **Special JS Features/Syntax (Not Mentioned)** There are no special JavaScript features or syntax mentioned in the provided code. If present, they would be worth discussing, especially if they could impact the performance comparison between the two approaches. **Alternatives** If you're looking for alternative filtering methods, consider: 1. **Native `every()` method**: Instead of using a combination of conditional statements and array methods, you can use the `every()` method to filter an array. 2. **Arrow functions with `includes()` or `some()`**: Using arrow functions in conjunction with `includes()` or `some()` might provide a more concise solution. In summary, the benchmark compares two approaches for filtering an array based on date conditions: the traditional `for` loop approach and the chained array approach utilizing optimized methods like `filter()` and `sort()`. The choice of approach depends on performance requirements, code readability, and personal preference.
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 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?