Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fight to the death - for loop vs chained (Dr Vacant - 1,000,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 < 1000000; 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 dive into the benchmark definition and explain what's being tested. **Benchmark Definition** The benchmark is testing two approaches to filter an array of objects based on a condition: 1. **For loop**: This approach uses a traditional `for` loop to iterate over the array, checking each object against a threshold value (`today`) to determine whether it belongs in the "past" or "future" arrays. 2. **Chained arr** (Array.prototype.filter() and Array.prototype.sort()): This approach uses the built-in `filter()` method to filter out objects with dates after `today`, followed by sorting the remaining array on the date property using `sort()`. Finally, it filters out objects with dates before `today` using another `filter()` call. **Options Compared** The benchmark is comparing the performance of these two approaches: * **For loop**: This approach uses a explicit loop to iterate over the array, which can be slower due to the overhead of creating and managing the loop variables. * **Chained arr**: This approach leverages built-in methods on the Array prototype, which are optimized for performance. **Pros and Cons** **For loop:** Pros: * Easy to understand and implement * Can be more control-oriented Cons: * May be slower due to overhead of creating and managing the loop variables * Less efficient than built-in array methods **Chained arr:** Pros: * Built-in methods are optimized for performance * Can take advantage of JavaScript engine optimizations Cons: * May have a steeper learning curve due to unfamiliar syntax * Can be less control-oriented **Other Considerations** The benchmark also tests the impact of using `switch` statements inside loops, which can lead to slower performance. **Library Usage** In this case, there is no explicit library usage mentioned. However, `Array.prototype.filter()` and `Array.prototype.sort()` are built-in methods that rely on libraries like V8 (used by Chrome) or SpiderMonkey (used by Firefox). The `Date` object and its methods are also part of the JavaScript standard library. **Special JS Features/Syntax** There is no special JavaScript feature or syntax used in this benchmark. It only uses standard JavaScript concepts, such as loops, array manipulation, and conditional statements. **Other Alternatives** If you wanted to implement this benchmark with alternative approaches, here are a few ideas: * Using `forEach()` instead of `for` loop * Using `map()` instead of filtering individual elements * Using a more control-oriented approach with recursive functions or functional programming techniques Keep in mind that these alternatives may not be as efficient as the built-in array methods used in the benchmark.
Related benchmarks:
fight to the death - for loop vs chained array methods (10)
fight to the death - for loop vs chained methods1 array length 100
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?