Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fight to the death - for loop vs chained methods 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].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):
Let's break down the benchmark and its various components. **Benchmark Definition** The benchmark is defined by two test cases: `for loop` and `chained arr`. The goal of the benchmark is to compare the performance of two approaches for filtering an array of objects based on a date condition. **Test Case 1: For Loop** The first test case uses a traditional `for` loop to iterate through the array and filter out elements. Here's what's happening: * An array `workArr` is created with 1 million objects, each containing a `startDate` property. * The test code pushes filtered objects into two arrays: `pastArr` and `futureArr`. * The filtering logic uses a `switch` statement to determine which array to push the object into based on its `startDate` value relative to the current date (`today`). **Test Case 2: Chained Arr** The second test case uses an array method chain to filter the same array. Here's what's happening: * The code creates a new array `firstFutureWorks` by filtering out elements with a `startDate` greater than the current date. * It then sorts this filtered array based on the `startDate` value. * If the first element of `firstFutureWorks` exists, it sets a `nextFutureDate` variable to its `startDate`. Otherwise, it sets it to the current date. * Finally, it filters out elements with a `startDate` greater than `nextFutureDate`. **Library and Purpose** In both test cases, no specific libraries are used. However, the `Array.prototype.sort()` method is used in the chained arr test case. **Special JS Features or Syntax** There's one notable feature: the use of the spread operator (`...`) to concatenate arrays in the `for loop` test case. **Pros and Cons** Here are some pros and cons for each approach: * **For Loop**: + Pros: - Easy to understand and implement. - Can be optimized with techniques like early returns or caching. + Cons: - Iterative, which can lead to slower performance compared to array method chaining. * **Chained Arr**: + Pros: - Can be more concise and readable. - Often faster due to the ability to build up intermediate results. + Cons: - Can be less intuitive for complex filtering scenarios. **Other Alternatives** Other approaches could include: * Using `Array.prototype.filter()` or `Array.prototype.map()` with additional libraries like Lodash or Ramda. * Employing more advanced techniques like SIMD (Single Instruction, Multiple Data) instructions or parallel processing using Web Workers. Keep in mind that the choice of approach depends on the specific use case and performance requirements.
Related benchmarks:
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 10,000
fight to the death - for loop vs chained methods 100,000
Comments
Confirm delete:
Do you really want to delete benchmark?