Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fight to the death - for loop vs chained RR2 (100000)
(version: 0)
Comparing performance of:
for loop vs chained arr
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const randomStDate = () => { const randD = new Date(new Date("2016-12-05").getTime() + Math.random() * (new Date("2030-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 dive into the world of JavaScript microbenchmarks on MeasureThat.net. The provided benchmark definition and individual test cases measure the performance difference between two approaches for filtering and sorting an array of objects based on their "startDate" property. **Benchmark Definition** The overall goal is to create a function that categorizes an array of objects (`workArr`) into three categories: past, present, and future, based on their `startDate` property. The test case uses a mix of for loops and chained array methods to achieve this. **Individual Test Cases** There are two individual test cases: 1. **"for loop"`**: This test case uses a traditional for loop to iterate through the `workArr` array and push or push elements into separate arrays (`pastArr` and `futureArr`) based on their `startDate` property. 2. **"chained arr"`**: This test case uses chained array methods (e.g., `filter()`, `sort()`) to achieve the same result as the for loop. **Options Compared** The two test cases compare the performance of: * A traditional for loop approach * A chained array method approach **Pros and Cons** Here's a brief overview of the pros and cons of each approach: **For Loop (Test Case: "for loop")** Pros: * Easy to understand and implement * Suitable for small to medium-sized datasets Cons: * Can be slower than chained array methods due to explicit loops * May not scale well with large datasets **Chained Array Methods (Test Case: "chained arr")** Pros: * Fast and efficient, especially for large datasets * Easy to implement and chain multiple methods together Cons: * Can be less intuitive to understand, especially for complex logic * May require more memory due to the creation of intermediate arrays **Library/Function Used** The `sort()` method is used in both test cases. This method sorts the elements of an array in place and returns the sorted array. **Special JavaScript Feature/Syntax** None mentioned. **Other Considerations** When working with large datasets, it's essential to consider factors like memory usage, caching, and optimization techniques to achieve better performance. In this case, the chained array method approach might be more suitable due to its efficiency, but careful consideration of the trade-offs is necessary. **Alternatives** Some alternative approaches could include: * Using `Array.prototype.forEach()` instead of a traditional for loop * Employing more efficient data structures like `PriorityQueue` or `Heap` * Utilizing modern JavaScript features like `Promise.all()` or `async/await` * Leveraging libraries like Lodash or Underscore.js for utility functions Keep in mind that the choice of approach ultimately 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 1,000,000
fight to the death - for loop vs chained RR3 (100000)
fight to the death - for loop vs chained RR (1000,000)
Comments
Confirm delete:
Do you really want to delete benchmark?