Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
MapIncludes vs Find on collection
(version: 0)
Compares two ways to find out if array contains item with value zero
Comparing performance of:
Map Includes vs Find
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var hasZero = []; var withoutZero = []; for (var i = 0; i < 10000; i++) { hasZero.push({ value: Math.floor(Math.random() * 1000) }); withoutZero.push({ value: Math.floor(Math.random() * 1000) + 1 }); }
Tests:
Map Includes
var tempResult = !!Math.round(Math.random()) ? hasZero.map(v => v.value).includes(v => v === 0) : withoutZero.map(v => v.value).includes(v => v === 0);
Find
var tempResult = !!Math.round(Math.random()) ? hasZero.find(v => v.value === 0) : withoutZero.find(v => v.value === 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map Includes
Find
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 provided benchmark and explain what is being tested, compared, and the pros/cons of different approaches. **Benchmark Definition** The benchmark measures two ways to check if an array contains an item with a value of zero: using `map()` followed by `includes()`, and using `find()`. **Script Preparation Code** The script generates two arrays, `hasZero` and `withoutZero`, each containing 10,000 objects with random values between 0 and 1000. The purpose of these arrays is to serve as test data for the benchmark. **Html Preparation Code** There is no HTML preparation code provided. **Individual Test Cases** The benchmark consists of two test cases: 1. **Map Includes**: This test case uses `map()` to transform each object in the array to its value, and then checks if any of these values are equal to 0 using `includes()`. 2. **Find**: This test case uses `find()` to search for an object with a value of 0 directly. **Library Used** There is no explicit library mentioned in the benchmark definition. However, `map()` and `find()` are built-in JavaScript functions that operate on arrays. **Special JS Features/Syntax** No special JavaScript features or syntax are used in this benchmark. **Comparison** The two test cases are compared to determine which approach is faster: * **Map Includes**: This method uses `map()` to transform each object's value, and then checks if any of these values are equal to 0 using `includes()`. * **Find**: This method uses `find()` to search for an exact match with a value of 0. **Pros/Cons** **Map Includes**: Pros: * More flexible: allows checking multiple conditions (e.g., equality with zero) * Can be used as part of a larger pipeline * May be faster if the array is large and the condition is complex Cons: * Creates a new array, which can be memory-intensive for large arrays * Performs more operations than `find()`: two map() calls followed by includes() **Find**: Pros: * More efficient: only performs one search operation * Faster than `map()` + `includes()` for small to medium-sized arrays Cons: * Only works if the exact value is present in the array * May not be as flexible as `map()` + `includes()` **Other Alternatives** Other approaches that could have been used include: * Using a custom algorithm or data structure (e.g., a binary search tree) * Utilizing the browser's built-in performance optimization features (e.g., WebAssembly) However, the use of `map()` and `find()` is a common and efficient approach for searching arrays in JavaScript.
Related benchmarks:
Some vs. Filter with pop() vs. indexOf vs. Includes vs. Find
Some vs. Filter vs. indexOf vs. Map+Includes vs. Find
Some vs. Filter vs. map+indexOf vs. map+Includes vs. Find
Some vs. Filter vs. indexOf vs. Includes vs. Find with fix
Comments
Confirm delete:
Do you really want to delete benchmark?