Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Contains, indexOf
(version: 4)
Comparing performance of:
includes vs indexOf
Created:
8 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var list = [...Array(1000).keys()].map((index) => {return {id: index, name: `PRODUCT_${index}`}}); var newItems = [{id: 50, name: 'CUST_50'}, {id: 412, name: 'CUST_412'}]; var newItemIds = newItems.map((item) => item.id);
Tests:
includes
list.filter((item) => { return !newItemIds.includes(item.id); });
indexOf
list.filter((item) => { return newItemIds.indexOf(item.id) === -1; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
indexOf
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 benchmark and its test cases. **What is being tested?** The provided JSON represents two microbenchmarks that compare the performance of two approaches: using the `includes` method and using the `indexOf` method to filter an array in JavaScript. The benchmarks are designed to measure the execution speed of these methods on a list of 1000 objects, where some items are already present in the list. **Options compared** The two test cases compare the performance of: 1. **Includes method**: This method checks if a specific item is present in an array using the `includes` function. 2. **IndexOf method**: This method returns the index of a specific item in an array, and then uses this index to determine if the item is present (if it's -1, the item is not present). **Pros and Cons** * **Includes method**: + Pros: Simple, efficient, and widely supported. + Cons: Can be slower than `indexOf` for large arrays because it requires iterating over the array until a match is found. * **IndexOf method**: + Pros: Can be faster than `includes` for large arrays because it returns an index immediately, reducing iteration time. Also, it's often more efficient for exact matches. + Cons: Requires a separate step to determine if the returned index is -1, which can add overhead. Other considerations: * The benchmarks are run on a Linux desktop with Chrome 76 browser, so the results might not be representative of other platforms or browsers. * The list size (1000) and item count (2 new items) are relatively small, which might affect the performance difference between `includes` and `indexOf`. * Both methods have a time complexity of O(n), where n is the array length. However, in practice, modern JavaScript engines often optimize these functions to be faster. **Library and syntax** Neither test case uses any external libraries or special JS features beyond standard JavaScript functionality. **Benchmark preparation code** The script preparation code creates an array `list` with 1000 objects, each representing a product, and adds two new items (`newItems`) that are not present in the list. The `newItemIds` variable is created by mapping the `id` property of `newItems` to an array. **Benchmark execution** The benchmark is executed multiple times (not specified), and the results are collected for each test case. The latest available result shows: * For the `includes` method, Chrome 76 on Linux desktop executes approximately 4827 executions per second. * For the `indexOf` method, Chrome 76 on Linux desktop executes approximately 4397 executions per second. **Alternatives** Other alternatives to these methods might include: * Using a Set data structure to keep track of present items, which can be faster for large arrays. * Implementing a custom filtering function using bitwise operations or other optimization techniques. * Using a different programming language or framework that provides more efficient array searching functionality.
Related benchmarks:
map vs fromentries
array.find vs object by key
find in array of objects by field 1002
make map with entries vs for of
simple array and map vs forEach vs for by cuteLuna v4
Comments
Confirm delete:
Do you really want to delete benchmark?