Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fight to the death - for loop vs chained array methods (10,000,000) minus sort
(version: 1)
remove the sort from for loop method
Comparing performance of:
for loop (removed the additional sort to make myself feel better) vs chained arr methods
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 (removed the additional sort to make myself feel better)
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 methods
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 (removed the additional sort to make myself feel better)
chained arr methods
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 provided JSON and explain what is tested on MeasureThat.net. **Benchmark Definition** The benchmark compares two approaches for filtering an array of objects based on their `startDate` property: a traditional `for` loop method and chained array methods (filtering and sorting). **Script Preparation Code** The script generates a large array (`workArr`) containing 100,000 objects with random `startDate` properties. The script also defines two empty arrays, `pastArr` and `futureArr`, which will be used to store filtered objects. **Options Compared** 1. **For Loop Method**: This approach uses a traditional `for` loop to iterate through the `workArr` array, checking each object's `startDate` property against the current date (`today`). Based on the comparison, the object is either added to `pastArr` or `futureArr`. 2. **Chained Array Methods (filtering and sorting)**: This approach uses chained methods (filter() and sort()) to process the `workArr` array. First, it filters out objects with a `startDate` greater than the current date (`today`). Then, if there are remaining objects, it sorts them based on their `startDate` property. **Pros and Cons of Each Approach** 1. **For Loop Method**: * Pros: Can be more efficient for large arrays since it avoids creating temporary sorted arrays. * Cons: Can be slower due to the overhead of the loop and individual comparisons. 2. **Chained Array Methods (filtering and sorting)**: * Pros: Faster since it leverages optimized JavaScript engine performance for filtering and sorting. * Cons: May create temporary sorted arrays, which can consume memory. **Library Used** None explicitly mentioned in the provided JSON. **Special JS Features or Syntax** The benchmark uses JavaScript's arrow functions (`const pl = () => { ... }`), destructuring assignment (`let [year, month, day] = randD.toISOString().split('-');`), and template literals (`'' + randD.getMonth() + 1;`). **Other Alternatives** If you're interested in exploring other approaches, here are a few options: * **Regular Expressions**: You could use regular expressions to filter the array, but this might not be as efficient or readable as the chained array methods approach. * **Set-Based Operations**: Another approach would be to use set-based operations (e.g., `Set` data structure) to efficiently filter and sort the array. However, this might require additional setup and understanding of set operations. Keep in mind that these alternatives might not provide a significant performance improvement over the chained array methods approach for this specific benchmark.
Related benchmarks:
fight to the death - for loop vs chained array methods (10)
fight to the death - for loop vs chained array methods (100,000)
fight to the death - for loop vs chained methods1
fight to the death - for loop vs chained methods1 array length 100
Comments
Confirm delete:
Do you really want to delete benchmark?