Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fight to the death - for loop vs chained methods1 array length 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].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):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark compares two approaches for sorting an array of objects based on their `startDate` property. **What is being tested?** The benchmark tests the performance difference between using a traditional `for` loop and chained methods (also known as method chaining) to filter and sort an array of objects. **Options compared** Two options are being compared: 1. **For Loop**: A traditional approach that uses a `for` loop to iterate through the array, filtering and sorting each element individually. 2. **Chained Arr**: An approach that uses chained methods (e.g., `filter()`, `sort()`) to filter and sort the entire array at once. **Pros and Cons** * **For Loop**: + Pros: Simple and easy to understand, can be optimized for specific use cases. + Cons: Can be slower due to the overhead of iterating through the array individually. * **Chained Arr**: + Pros: Can be faster since it processes the entire array at once, reducing overhead. + Cons: May be less intuitive and harder to optimize. **Library** The benchmark uses the `Date` object and the `Math` library, which are built-in to JavaScript. No external libraries are required. **Special JS feature or syntax** There is no special JS feature or syntax used in this benchmark beyond what's typical for filtering and sorting arrays using modern JavaScript methods. **Other alternatives** Other approaches that could be used to sort this array include: * Using a `forEach` loop with an arrow function * Utilizing the `Array.prototype.every()` method * Leveraging a library like Lodash or Moment.js (although not necessary in this simple example) However, for this specific benchmark, using chained methods is likely to provide better performance due to its ability to process the entire array at once. **Benchmark Preparation Code** The provided script preparation code creates an array of 100 objects with `startDate` properties and a random `endDate`. The HTML preparation code is empty, indicating that no specific HTML-related tests are being run.
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 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?