Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.map().includes() vs .some()
(version: 1)
Comparing performance of:
.some() vs .map().includes()
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
var data = [] for (let i = 0; i < 5000; ++i) data.push({ username: 'toto' }) data.push({ username: 'titi' }) for (let i = 0; i < 2500; ++i) data.push({ username: 'toto' })
Tests:
.some()
data.some((e) => e.username === 'titi')
.map().includes()
data.map((e) => e.username).includes('titi')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
.some()
.map().includes()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.some()
126018.2 Ops/sec
.map().includes()
24826.2 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark compares the performance of two different approaches for checking if an element exists in an array of objects in JavaScript. The specific methods being tested are `.some()` and the combination of `.map().includes()`. ### Options Compared: 1. **`.some()` Method**: - **Test Case**: `data.some((e) => e.username === 'titi')` - **Purpose**: The `.some()` method tests whether at least one element in the array passes the test implemented by the provided function. It returns `true` as soon as it finds an element that meets the condition, otherwise it returns `false`. 2. **`.map().includes()` Combination**: - **Test Case**: `data.map((e) => e.username).includes('titi')` - **Purpose**: This approach first transforms the original array of objects into an array of usernames using the `.map()` method. Then it checks if the username 'titi' exists in this new array using the `.includes()` method. ### Pros and Cons: **`.some()` Method**: - **Pros**: - Efficient for this use case since it stops searching as soon as a match is found, which can reduce execution time, particularly with large data sets. - Directly checks the condition without needing to create additional arrays. - **Cons**: - If you need to perform additional operations or transformations on the elements, you'll need to handle them separately. **`.map().includes()` Combination**: - **Pros**: - Can be more readable for developers familiar with functional programming, as it clearly separates the mapping and filtering steps. - Useful if you need the transformed array for additional logic beyond existence checking. - **Cons**: - Less performant in this scenario because it creates a new array containing all usernames before checking for inclusion, consuming more memory and processing. - Always processes the entire array to create the new array, even if a matching username is found early in the list, which adds unnecessary overhead. ### Other Considerations: - Memory Usage: `.map().includes()` requires additional memory for storing the transformed array, which can be a significant factor in cases with large datasets. - Readability vs. Performance: While `.map().includes()` can enhance readability for some engineers, it sacrifices performance in a straightforward presence check, especially evident in the benchmark results where `.some()` executed significantly more times per second than the `.map().includes()` approach. ### Benchmark Results: The benchmark results reflect these differences clearly: - The `.some()` method achieved an impressive 248,658.14 executions per second. - Meanwhile, the `.map().includes()` method only managed 26,016.13 executions per second, highlighting the performance cost associated with creating an intermediate array. ### Alternatives: In addition to the two methods tested: - **`.filter()`**: This method could also be used, allowing filtering based on the condition, but is less efficient for this purpose than `.some()`. - **For Loop**: A traditional `for` loop can be quite performant as well for simple checks, allowing for early exits upon finding a match like `.some()`. - **Set Data Structure**: If looking up elements is a frequent operation, maintaining a Set based on usernames could vastly optimize membership checks, providing average time complexity of O(1) for lookups. Understanding these differences enables developers to make informed choices based on their specific needs for performance, readability, and memory efficiency.
Related benchmarks:
Some vs Find
Some vs !!Find
Some vs Find II
Some vs Find small sample
Some vs Find for non existence check
Some vs Find atata
Map Includes vs Some Simplified
Some vs Find test2
Some vs Find bool
Comments
Confirm delete:
Do you really want to delete benchmark?