Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
40 x 2000 - includes vs find
(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, ...l2]); var l = [...l1, ...l2]; 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((heart) => heart.sin === p.sin); return isHearted; })
has
l3.map(p => { const isHearted = !!s?.has((heart) => heart.sin === 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 break down the provided benchmark and explain what's being tested, compared, and discussed. **Benchmark Overview** The provided JSON represents two benchmark tests: 1. **Script Preparation Code**: This code is executed once to set up the environment for the benchmarks. It creates three arrays (`l1`, `l2`, and `l3`) with 1000 random objects each. Then, it converts these arrays into sets (`s`), lists (`l`), and another list (`l3`). 2. **Individual Test Cases**: These are separate tests that run on the prepared data. **Test Cases** There are three individual test cases: 1. `find` 2. `includes` 3. `has` Each test case maps an array element (`p`) to a boolean value indicating whether it's present in the original arrays (`l`, `s`, or `l3`). The goal is to measure which approach is faster. **Approaches Compared** The three approaches being compared are: 1. **`find()`**: This method searches for an element within an array by calling `Array.prototype.find()`. 2. **`includes()`**: This method checks if an element exists in an array without returning the element itself, using `Array.prototype.includes()`. 3. **`has()`** (via `Set`): This method uses a Set to check for existence. **Pros and Cons** Here's a brief summary of each approach: 1. **`find()`**: * Pros: Simple and intuitive. * Cons: Can be slower due to the additional search step. 2. **`includes()`**: * Pros: Faster than `find()`, as it only needs to scan until finding the element or reaching the end of the array. * Cons: Returns the found element, which might not be necessary in all cases. 3. **`has()`** (via `Set`): * Pros: Can be very fast for large datasets using Sets. * Cons: Requires converting data to a Set initially, and the `has()` method is only available on modern browsers. **Library/Functionality Used** None of the test cases explicitly use any external libraries. However, the `find()`, `includes()`, and `has()` methods are built-in JavaScript functions. **Special JS Feature/Syntax** No special JavaScript features or syntax are used in this benchmark. **Alternatives** If you're interested in exploring alternative approaches, here are a few: * For `find()`: Use `Array.prototype.findIndex()` instead, which returns the index of the element if found. * For `includes()`**: Consider using a library like `fast-includes` or implementing a custom implementation for better performance. Keep in mind that these alternatives might not be applicable depending on your specific use case and target browser environments.
Related benchmarks:
Random I/O various arrays
40 x 2000 - includes vs find 2
dealing with array of array which array should come first?
Lodash _.union vs the native Set() for multiple arrays
Comments
Confirm delete:
Do you really want to delete benchmark?