Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array maps
(version: 0)
Comparing performance of:
map vs filterOnAir
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var hasZero = []; var toFilter = ["run_8","run_3","run_4","run_5","run_15","run_345"]; for (var i = 0; i < 10000; i++) { hasZero.push({ run: i.toString(), name: "run_" + i }); }
Tests:
map
var newArray = hasZero.map(({name}) => name) var filteredArray = newArray.filter(name => toFilter.some(item => item === name));
filterOnAir
var filteredArray = hasZero.filter(({name}) => toFilter.some(item => item === name));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
map
filterOnAir
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 benchmark and explain what's being tested. **Benchmark Overview** The benchmark tests two common methods for filtering an array in JavaScript: `map` and `filter`. The test case creates a large array of objects with "run" properties, which are then used to filter another array containing specific strings. **Options Compared** Two options are compared: 1. **`map`**: This method creates a new array by applying the provided callback function to each element in the original array. In this case, it extracts only the "name" property from each object. 2. **`filterOnAir`**: This is an internal implementation of `filter` used by Firefox. It's not a standard JavaScript method. **Pros and Cons** * **`map`**: + Pros: More intuitive and readable code, doesn't modify the original array. + Cons: Creates a new array, which can be memory-intensive for large datasets. * **`filterOnAir`**: + Pros: Internal implementation might be optimized for Firefox's use case. + Cons: Not a standard JavaScript method, making it less familiar to developers. **Library and Purpose** The `some()` method is used in the filter conditions. It returns `true` if at least one element of the array passes the test implemented by its callback function. **Special JS Feature/Syntax** There's no special feature or syntax being tested in this benchmark. The focus is on comparing two common methods for filtering arrays. **Other Alternatives** In addition to `map` and `filter`, there are other ways to filter an array in JavaScript, such as: * Using `forEach()` with a callback function * Using `reduce()` with a callback function * Using a custom loop and conditional statements However, these methods might not be as efficient or readable as the standard `filter` method. **Benchmark Preparation Code** The script preparation code creates an array of 10,000 objects with "run" properties and assigns it to the variable `hasZero`. Another array, `toFilter`, contains specific strings that will be used for filtering. The script then uses a loop to populate `hasZero` with random "run" values. **Individual Test Cases** The benchmark consists of two test cases: 1. **`map`**: Tests the `map` method by creating a new array from `hasZero`, extracting only the "name" property, and then filtering the resulting array using `some()`. 2. **`filterOnAir`**: Tests the internal implementation of `filter` used by Firefox (`filterOnAir`) on the same data. **Latest Benchmark Result** The benchmark results show the executions per second for each test case: * `map`: 4110.51220703125 executions/second * `filterOnAir`: 3169.5615234375 executions/second These results suggest that `map` is slightly faster than the internal implementation of `filter` used by Firefox in this specific use case.
Related benchmarks:
Some, Filter, indexOf
Some, Filter, indexOf.
Filter vs. Find
index vs map115
Comments
Confirm delete:
Do you really want to delete benchmark?