Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array filter vs. for loop - with for in zz
(version: 0)
Comparing performance of:
Filter vs For vs In place for vs in place, temp var for value vs filter, arrow funcs
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = Array.from(Array(100000), () => (Math.random() * 10) |0)
Tests:
Filter
arr.filter(function(item) { return (item>4); });
For
let farr=[]; for (var i=0,len=arr.length;i<len;i++) { if (arr[i]>4) farr.push(arr[i]); }
In place for
let farr=[]; let next = 0; for (var i=0,len=arr.length;i<len;i++) { if (arr[i]>4) farr[next++] = arr[i]; } farr.splice(next);
in place, temp var for value
let farr=[]; let next = 0; for (var i=0,len=arr.length;i<len;i++) { let val = arr[i] if (val>4) farr[next++] = val; } farr.splice(next);
filter, arrow funcs
arr.filter(item => item>4)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Filter
For
In place for
in place, temp var for value
filter, arrow funcs
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):
Measuring JavaScript performance! Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares four different approaches to filter an array of 100,000 elements: 1. `Array.prototype.filter()` 2. A traditional `for` loop 3. An in-place `for` loop with a temporary variable 4. An arrow function version of the `filter()` method **Tested Options** The benchmark tests these four options against each other to determine which one performs best. Pros and Cons: * **Array.prototype.filter()**: This is a built-in method that creates a new array with all elements that pass the test implemented by the provided function. It's concise and readable, but may incur additional memory overhead. * **Traditional `for` loop**: This approach iterates over the array using an index variable, checking each element against the condition. It's more control-oriented but can be less efficient due to the need for manual indexing. * **In-place `for` loop with a temporary variable**: This variant uses a single variable to store filtered elements and avoids creating a new array. However, it may introduce additional overhead due to the need to manage the variable. * **Arrow function version of filter()**: This is a concise and readable way to implement the `filter()` method using an arrow function. It's likely to be comparable in performance to the built-in implementation. **Library Usage** None of the benchmarked options explicitly use any external libraries, but it's worth noting that JavaScript engines may have internal optimizations or micro-optimizations that affect performance. **Special JS Feature/Syntax** The benchmark uses a feature called "template literals" (`\r\n`) in the `Script Preparation Code` section. This is a modern JavaScript syntax that allows for more readable and efficient string interpolation. However, older JavaScript engines might not support it or may have different behavior. **Other Alternatives** There are other ways to filter an array, such as: * Using `Array.prototype.map()` and `Array.prototype.filter()` * Using `Set` data structure to keep track of unique elements * Using a custom implementation with bitwise operations (although this is likely to be less efficient) The benchmark provides a controlled comparison of these approaches, allowing users to understand the performance characteristics of each option. When interpreting the results, consider factors such as: * Memory usage: How much memory does each approach allocate? * Execution time: What's the performance impact of each approach on your specific use case? Keep in mind that the benchmark is designed to provide a general understanding of performance differences between these approaches. The actual performance impact will depend on your specific code, hardware, and JavaScript engine version.
Related benchmarks:
slice vs filter more than 1000
slice vs filter (10000000)
Set from array vs array Filter unique
slice vs filter lzg
slice vs filter for index filtering
Comments
Confirm delete:
Do you really want to delete benchmark?