Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filter array using includes vs object lookup
(version: 0)
Comparing performance of:
Filter using array includes vs Filter using object lookup
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] var b = ['h', 'i', 'j']
Tests:
Filter using array includes
a.filter((item) => !b.includes(item))
Filter using object lookup
const obj_b = b.reduce((acc, item) => { acc[item] = true return acc }, {}) a.filter((item) => !obj_b[item])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Filter using array includes
Filter using object lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Filter using array includes
2248749.5 Ops/sec
Filter using object lookup
4946202.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided benchmark. The benchmark is designed to compare two approaches for filtering an array: using `Array.prototype.includes()` and using object lookup (specifically, creating an object with the elements as keys and setting its value to `true`). **Options Compared:** 1. **Array includes**: Using the `includes()` method on the `Array.prototype`, which checks if a specified element is present in the array. 2. **Object lookup**: Creating an object (`obj_b`) with the elements of the second array as keys and setting its value to `true`. Then, using this object to check if an element is not present in the first array. **Pros and Cons:** * **Array includes**: + Pros: Simple, concise, and widely supported. + Cons: May be slower than object lookup for large arrays due to the overhead of searching the array. * **Object lookup**: + Pros: Can be faster for large arrays since it uses a hash table, which allows for O(1) lookups. Also, can handle nested objects or more complex filtering logic. + Cons: Requires creating an object and accessing its properties, which may add overhead for small arrays. In general, the choice between array includes and object lookup depends on the specific use case: * Use `Array.prototype.includes()` when you need to filter an array in a simple, one-time operation with a relatively small dataset. * Use object lookup when you need to perform more complex filtering or need to handle large datasets efficiently. **Library/Module Used:** The benchmark uses JavaScript's built-in `Array.prototype` and the `reduce()` method from Array prototype. No external libraries are required. No special JavaScript features or syntax are used in this benchmark. Now, let's talk about alternatives: 1. **Using `Set`**: Instead of creating an object with elements as keys, you can use a `Set` data structure, which provides similar performance benefits. 2. **Iterating manually**: You could iterate through the array and check each element manually, but this would likely be slower than both the array includes and object lookup approaches. Keep in mind that these alternatives might not provide significant performance improvements over the original methods, but they can offer different trade-offs or be more suitable for specific use cases.
Related benchmarks:
IndexOf vs Includes
IndexOf vs Includes vs find
set.has vs. array.includes 2333
equality vs includes
equals vs includes
Comments
Confirm delete:
Do you really want to delete benchmark?