Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test array
(version: 0)
Comparing performance of:
1 vs 2 vs 3
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(1000000).fill(Math.random());
Tests:
1
const filtedArr = array.map(x => x % 2 === 0);
2
const filtedArr = [] for(const x of array) { if(x % 2 === 0) { filtedArr.push(x); } }
3
const filtedArr = array.filter(x => x % 2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
1
2
3
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):
I'll break down the provided benchmark test cases and explain what's being tested, compared, and considered. **Benchmark Overview** The test case measures the performance of three different approaches to filter an array of 1 million elements in JavaScript: 1. Using `Array.prototype.map()` with a callback function. 2. Using a traditional `for` loop to iterate over the array and push filtered elements into a new array. 3. Using `Array.prototype.filter()` with a callback function. **Script Preparation Code** The script preparation code generates an array of 1 million random numbers using `new Array(1000000).fill(Math.random())`. **Individual Test Cases** Each test case represents a separate benchmark: 1. **Test Case 1: Using `map()`** ```javascript const filtedArr = array.map(x => x % 2 === 0); ``` This test case uses the `map()` method to create a new array with only even numbers from the original array. **Test Case 2: Using Traditional Loop** ```javascript const filtedArr = []; for (const x of array) { if (x % 2 === 0) { filtedArr.push(x); } } ``` This test case uses a traditional `for...of` loop to iterate over the original array and push filtered elements into a new array. **Test Case 3: Using `filter()`** ```javascript const filtedArr = array.filter(x => x % 2); ``` This test case uses the `filter()` method to create a new array with only numbers from the original array that satisfy the condition `x % 2`. **Benchmark Results** The latest benchmark results show the execution time (in executions per second) for each test case on a Chrome Mobile 92 browser, Android operating system, and mobile device. **Comparison** The tests compare the performance of three different approaches: * **`map()`**: A concise and expressive way to filter an array. * **Traditional Loop**: A more verbose approach that requires manual iteration and indexing. * **`filter()`**: Another concise and expressive way to filter an array, similar to `map()`. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **`map()`**: * Pros: Concise, expressive, and easy to read. * Cons: May be slower due to the overhead of creating a new array. 2. **Traditional Loop**: * Pros: Can be faster for large arrays, as it avoids the overhead of creating a new array. * Cons: More verbose, harder to read, and error-prone. 3. **`filter()`**: * Pros: Concise, expressive, and easy to read. * Cons: May be slower than `map()` due to the additional overhead of filtering. **Library and Special Features** None of these test cases use any external libraries or special JavaScript features beyond the standard array methods (`map()`, `filter()`, and `for...of` loop). **Other Alternatives** If you're looking for alternative approaches, consider: 1. **Using a library**: There are many JavaScript libraries (e.g., Lodash) that provide additional filtering functions with optimizations. 2. **Using a Just-In-Time (JIT) compiler**: Some browsers and engines use JIT compilation to optimize performance-critical code. 3. **Using parallel processing or concurrency**: Modern JavaScript engines can take advantage of multi-core processors and asynchronous execution to improve performance. Keep in mind that the best approach depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
Fill array with random integers
.at vs [x]
at 500 vs [500]
Array fill method vs for loop
fill vs manual fill
Comments
Confirm delete:
Do you really want to delete benchmark?