Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fight to the death - for loop vs chained (10)
(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 < 10; 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 MeasureThat.net and explore what's being tested in this benchmark. **Benchmark Definition** The benchmark is designed to compare two approaches for sorting an array of objects based on their "startDate" property. The test case, titled "fight to the death - for loop vs chained (10)", uses a dataset of 10 objects with random start dates and other properties. **Options being compared:** 1. **For Loop Approach**: This approach iterates through the `workArr` array using a traditional `for` loop, sorting and filtering the data sequentially. 2. **Chained Array Approach**: This approach leverages the `filter()` and `sort()` methods of the Array prototype in JavaScript, allowing for more concise and efficient iteration over the data. **Pros and Cons:** **For Loop Approach:** Pros: * Can be easier to understand and modify for developers familiar with traditional loops. * Allows for more explicit control over iteration and filtering. Cons: * Can be slower due to the overhead of sequential iteration and branching. * May not be as concise or readable as other approaches. **Chained Array Approach:** Pros: * More concise and expressive, making it easier to read and maintain. * Can be faster due to the optimized implementation of `filter()` and `sort()` methods. Cons: * Requires a good understanding of JavaScript's array prototype methods. * May not be as intuitive for developers unfamiliar with these methods. **Library and Special JS Feature:** In this benchmark, the `Date` object is used extensively, which is a built-in JavaScript library. The `Date` object provides methods for working with dates, such as `getTime()`, `getMonth()`, and `getFullYear()`. There are no special JavaScript features or syntax used in this benchmark that would require additional explanation. **Other Considerations:** * The benchmark uses a relatively small dataset (10 objects), which may not be representative of larger datasets. * The test case assumes that the `startDate` property is unique among the array elements, which might not always be the case in real-world scenarios. **Alternatives:** If you're looking for alternatives to MeasureThat.net, here are a few options: 1. **jsbench**: A benchmarking platform specifically designed for JavaScript performance testing. 2. **BenchmarkDotNet**: An open-source benchmarking library for .NET and JavaScript. 3. **JSTestDriven**: A tool for running JavaScript tests and benchmarks in your development environment. These alternatives offer more features, flexibility, and scalability compared to MeasureThat.net, but may require more setup and configuration.
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 methods1 array length 100
fight to the death - for loop vs chained methods 10,000
fight to the death - for loop vs chained methods 1,000,000
Comments
Confirm delete:
Do you really want to delete benchmark?