Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes vs lodash includes vs dictionary with large array
(version: 0)
Banana
Comparing performance of:
IndexOf vs Includes vs lodash vs dictionary
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
function makeid(length) { let result = ''; const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; const charactersLength = characters.length; let counter = 0; while (counter < length) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); counter += 1; } return result; } var array = []; var ver_dictionary = {}; for(let i = 0; i < 50000; i++) { const cur = makeid(10); array.push(cur); ver_dictionary[cur] = true; }
Tests:
IndexOf
array.indexOf(array[Math.floor(Math.random()*50000)]) !== 1
Includes
array.includes(array[Math.floor(Math.random()*50000)])
lodash
_.includes(array, array[Math.floor(Math.random()*50000)])
dictionary
array[Math.floor(Math.random()*50000)] in ver_dictionary
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
IndexOf
Includes
lodash
dictionary
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 its test cases. **Benchmark Overview** The benchmark measures the performance of different approaches for checking if an element is present in an array: 1. `array.indexOf(array[Math.floor(Math.random()*50000)])` 2. `array.includes(array[Math.floor(Math.random()*50000)])` 3. `_.includes(array, array[Math.floor(Math.random()*50000)])` (using the Lodash library) 4. `array[Math.floor(Math.random()*50000)] in ver_dictionary` **Options Compared** The benchmark compares four different approaches: 1. **Native Array Methods**: `indexOf()` and `includes()` * Pros: Fast, efficient, and widely supported. * Cons: May not be as performant for very large arrays or specific edge cases. 2. **Lodash Includes Function** * Pros: Convenient, easy to use, and well-tested library. * Cons: Adds extra overhead due to the use of a separate library. 3. **Dictionary-Based Approach** * Pros: Simple, efficient, and can be optimized for large arrays. * Cons: May not be as straightforward or intuitive as native array methods. **Library Usage** The benchmark uses the Lodash library, which is a popular utility library that provides many functional programming helpers. In this case, the `_.includes()` function is used to check if an element is present in the array. The Lodash library adds extra overhead due to its dynamic loading and compilation process, but it also provides a convenient and well-tested way to perform common operations. **Special JS Features or Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. All test cases use standard JavaScript functions and syntax. **Benchmark Result Interpretation** The latest benchmark result shows the performance metrics for each test case: * `ExecutionsPerSecond` measures the number of executions per second on the device. * The results show that the dictionary-based approach (`array[Math.floor(Math.random()*50000)] in ver_dictionary`) outperforms the native array methods (`indexOf()` and `includes()`) by a significant margin. **Other Alternatives** Some alternative approaches to consider: 1. **Binary Search**: Instead of linear searches, you could use binary search algorithms (e.g., `array.indexOf(array[Math.floor(Math.random()*50000)])` with a modified implementation) for even faster performance. 2. **Hash Tables**: You could use hash tables or object-based data structures (e.g., `ver_dictionary`) to store the array elements and check membership more efficiently. 3. **Parallelization**: If you're running multiple instances of this benchmark, you could consider parallelizing the execution using worker threads, async/await, or other concurrency techniques. These alternatives might not be directly relevant to the original benchmark but can provide food for thought on how to further optimize or approach similar performance-critical code.
Related benchmarks:
Lodash _.some vs _.includes vs array.find
Lodash sort vs array.prototype.sort for objects with strings
toLowerCase() Sorting
Better set.has vs array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?