Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
40 x 2000 - includes vs find 2
(version: 0)
Comparing performance of:
find vs includes vs has
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var l1 = []; var l2 = [] for (var i = 0; i < 1000; i++) { l1.push({ sin: Math.floor(Math.random() * 1000).toString(), col: "c" + Math.floor(Math.random() * 1000) }); } for (var j = 0; j < 1000; j++) { l2.push({ sin: Math.floor(Math.random() * 1000).toString(), col: "c" + Math.floor(Math.random() * 1000) }); } var s = new Set([...l1.map(x => x.sin), ...l2.map(x => x.sin)]); var l = [...l1.map(x => x.sin), ...l2.map(x => x.sin)]; var l3 = []; for (var k = 0; k < 40; k++) { l3.push({ sin: Math.floor(Math.random() *1000).toString()}); }
Tests:
find
l3.map(p => { const isHearted = !!l?.find((heart) => heart.sin === p.sin); return isHearted; })
includes
l3.map(p => { const isHearted = !!l?.includes(p.sin); return isHearted; })
has
l3.map(p => { const isHearted = !!s?.has(p.sin); return isHearted; })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
find
includes
has
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 dive into the world of JavaScript microbenchmarks! **Benchmark Definition** The provided JSON represents a benchmark definition for measuring the performance of three different approaches: `find`, `includes`, and `has`. The benchmark creates two arrays, `l1` and `l2`, containing random objects with `sin` properties. It then uses these arrays to create sets (`s`) and iterate over them. The main difference between the test cases is the way they search for a specific value in the sets: * `find`: Searches for an exact match using the `find()` method. * `includes`: Checks if a value exists in the set using the `includes()` method. * `has`: Checks if a value exists in the set using the `has()` method. **Options Compared** The three options are compared to determine which one is the fastest. The benchmark measures the time taken by each approach to execute the mapping function on the third array, `l3`. **Pros and Cons of Each Approach** 1. **find**: This approach uses a linear search algorithm, which can be slow for large datasets. However, it provides an exact match. * Pros: Exact match, straightforward implementation. * Cons: Slow for large datasets. 2. **includes**: This approach checks if the value exists in the set using a lookup table. It's faster than `find` but still slower than `has`. * Pros: Faster than `find`, uses less memory. * Cons: Checks if the value exists, not an exact match. 3. **has**: This approach is similar to `includes` but returns a boolean result directly, without creating a lookup table. * Pros: Fastest option, provides an exact match with minimal overhead. * Cons: Requires additional memory allocation. **Library Used** The benchmark uses the following libraries: 1. `Set`: A built-in JavaScript object that stores unique values in a set data structure. 2. `Array.prototype.map()`: A built-in JavaScript method for creating a new array from an existing one. **Special JS Feature/Syntax** None of the benchmark code utilizes any special JavaScript features or syntax, such as async/await, promises, or modernization APIs like `let` and `const`. The code is straightforward and focused on illustrating the performance differences between the three options. **Alternatives** If you need to optimize set lookups in your application, consider using: 1. **Bloom filters**: A space-efficient data structure that can quickly determine whether an element exists in a set. 2. **Trie data structures**: A specialized tree-based data structure optimized for string matching and substring searching. Keep in mind that the choice of optimization depends on the specific use case, performance requirements, and trade-offs between memory usage, lookup time, and implementation complexity.
Related benchmarks:
Lodash.js vs Native Remove Duplicates
40 x 2000 - includes vs find
dealing with array of array which array should come first?
Map clearance
Comments
Confirm delete:
Do you really want to delete benchmark?