Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fight to the death - for loop vs chained (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]
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):
Let's break down what is tested in this benchmark. **Benchmark Definition** The benchmark compares the performance of two approaches to filter an array and return a subset of elements based on a condition: 1. **For loop approach**: The code uses a traditional `for` loop to iterate over the array, and inside the loop, it checks the condition for each element using a `switch` statement. 2. **Chained array approach**: The code uses the `filter()` and `sort()` methods of the Array prototype to filter and sort the array in a single pipeline. **Options compared** The benchmark compares the performance of these two approaches on an array of 100,000 elements. **Pros and cons of each approach:** * **For loop approach**: + Pros: Direct control over iteration and loop logic. + Cons: Can be slower due to the overhead of looping and branching. * **Chained array approach**: + Pros: Faster and more concise, as it leverages optimized Array prototype methods. + Cons: Less control over iteration and logic, which might be a disadvantage for complex filtering scenarios. **Library and syntax** The benchmark uses JavaScript's built-in `Date` object and the `Array.prototype.filter()` and `Array.prototype.sort()` methods. These are part of the ECMAScript standard and do not require any additional libraries. **Special JS feature or syntax** There is no special JS feature or syntax used in this benchmark that would affect its performance or interpretation. **Other alternatives** If you want to experiment with other approaches, here are a few ideas: * **Regex**: Use regular expressions to filter the array. This can be faster than the `filter()` method but might be less efficient for large datasets. * **Native methods (e.g., `Array.prototype.reduce()`)**: Try using native methods like `reduce()` instead of `filter()` and `sort()`. This might offer better performance, but its complexity might outweigh any potential gains. Keep in mind that the choice of approach depends on the specific requirements and constraints of your project.
Related benchmarks:
fight to the death - for loop vs chained array methods (10)
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 100,000
fight to the death - for loop vs chained methods 1,000,000
Comments
Confirm delete:
Do you really want to delete benchmark?