Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sort testing lol
(version: 0)
Comparing performance of:
For vs Foreach vs Foreach + push + concat vs Filter + concat
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const ARR_NUMBERS = 50 window.array = new Array(ARR_NUMBERS) for(let i = 0 ; i < ARR_NUMBERS; i+=1){ window.array[i] = i; }
Tests:
For
const evenArray = new Array(window.array.length) let evenArrayIndex = 0 const oddArray = new Array(window.array.length) let oddArrayIndex = 0 for(let index = 0 ; index < window.array.length; index +=1){ const value = window.array[index] if(value % 2 ===0){ evenArray[evenArrayIndex] = value; evenArrayIndex += 1; }else{ oddArray[oddArrayIndex] = value; oddArrayIndex += 1; } } for(let index = 0; index < oddArrayIndex; index +=1){ const value = oddArray[oddArrayIndex] evenArray[evenArrayIndex] = value; evenArrayIndex += 1 }
Foreach
const evenArray = new Array(window.array.length) let evenArrayIndex = 0 const oddArray = new Array(window.array.length) let oddArrayIndex = 0 window.array.forEach(value =>{ if(value % 2 ===0){ evenArray[evenArrayIndex] = value; evenArrayIndex += 1; }else{ oddArray[oddArrayIndex] = value; oddArrayIndex += 1; } }) oddArray.forEach(value=>{ evenArray[evenArrayIndex] = value; evenArrayIndex += 1 })
Foreach + push + concat
const evenArray = new Array() const oddArray = new Array() window.array.forEach(value =>{ if(value % 2 ===0){ evenArray.push(value); }else{ oddArray.push(value); } }) const x = evenArray.concat(oddArray)
Filter + concat
const evenArray = window.array.filter(v=> v%2 === 0) const oddArray = window.array.filter(v=> v%2 !== 0) const x = evenArray.concat(oddArray)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
For
Foreach
Foreach + push + concat
Filter + concat
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):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON data represents the benchmark definition, individual test cases, and latest benchmark results. **Benchmark Definition** The benchmark definition provides information about the script preparation code and HTML preparation code (although it's empty in this case). In general, the script preparation code sets up a variable or an array that will be used to generate a large dataset for testing. The HTML preparation code is not relevant to this benchmark. In this specific case, the script preparation code generates an array of 50 numbers from 0 to 49 and assigns each number to a corresponding index in the `window.array` object. **Individual Test Cases** There are four test cases: 1. **For**: This test case uses a traditional for loop to separate even and odd numbers into two arrays. * Pros: Clear, straightforward approach that is easy to understand and implement. * Cons: May not be the most efficient way to separate even and odd numbers, especially for large datasets. 2. **Foreach + push + concat**: This test case uses a foreach loop with push and concat methods to generate the even and odd arrays. * Pros: Uses built-in array methods that are commonly used in JavaScript development, making it easier to understand and implement. * Cons: May not be as efficient as other approaches due to the overhead of concatenating arrays. 3. **Foreach + push + concat ( alternative implementation)**: This test case is identical to the previous one, but with a slight variation in the script preparation code. * Pros: Same advantages and disadvantages as the previous test case. 4. **Filter + concat**: This test case uses the Array.prototype.filter() method to separate even and odd numbers into two arrays. * Pros: More concise and expressive than traditional for loops, making it easier to read and maintain. * Cons: May be less efficient than other approaches due to the overhead of filtering arrays. **Other Considerations** All test cases use the `window.array` object as a shared dataset, which may lead to performance variations depending on the implementation details. The **For** test case is likely the slowest due to the explicit loop iteration, while the **Filter + concat** test case might be faster due to the optimized filtering algorithm used by modern browsers. The **Foreach + push + concat** test case falls somewhere in between due to the overhead of concatenating arrays. **Libraries and Special Features** None of the test cases explicitly use any libraries or special features that require a deep understanding of JavaScript. However, they do rely on built-in array methods like `forEach()`, `push()`, `concat()`, and `filter()`. **Alternatives** Other approaches to separate even and odd numbers could include: * Using a single array with two indices (one for even and one for odd) and updating the corresponding index based on the number value. * Using a custom implementation of the Sieve of Eratosthenes algorithm to generate even and odd arrays. * Utilizing parallel processing or multi-threading to separate numbers in parallel. However, these alternative approaches would likely require significant modifications to the benchmark code and may not be as straightforward to understand or implement.
Related benchmarks:
Array.sort() vs Math.min / Math.max 4 elements
Javascript Sorting Algorithms FRAC
Sorting of array
Sorting for loop vs array.sort
Comments
Confirm delete:
Do you really want to delete benchmark?