Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs Dictionary lookup
(version: 0)
Comparing performance of:
Array.includes vs Dictionary lookup
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
Script Preparation code:
var myArr = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']; var myDict = _.keyBy(myArr);
Tests:
Array.includes
if (myArr.includes('m')) { console.log("array has 'm'"); }
Dictionary lookup
if (myDict['m']) { console.log("dict has 'm'"); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.includes
Dictionary lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0
Browser/OS:
Chrome 139 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.includes
480324.0 Ops/sec
Dictionary lookup
504612.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **What is being tested?** The provided JSON represents a JavaScript microbenchmark that compares two approaches: using `Array.includes()` to search for an element in an array, and using dictionary (object) lookup to achieve the same result. The benchmark aims to determine which approach is faster. **Options compared:** 1. **Array.includes()**: This method searches for an element in the array and returns a boolean value indicating whether the element exists. 2. **Dictionary lookup**: This method uses an object to store key-value pairs, where each key corresponds to a specific element in the original array. The benchmark uses Lodash's `_.keyBy()` function to create this dictionary. **Pros and Cons:** * **Array.includes()**: + Pros: - Easy to understand and implement. - Widely supported across browsers. + Cons: - Can be slower due to the linear search algorithm used by default (although some modern browsers optimize it). * **Dictionary lookup**: + Pros: - Generally faster, especially for larger datasets, since it uses a hash table-based search. + Cons: - Requires creating an additional object and potentially more memory usage. The choice between these two approaches depends on the specific use case and performance requirements. If speed is critical, dictionary lookup might be a better option. However, if simplicity and ease of implementation are prioritized, `Array.includes()` could be sufficient. **Library used:** * **Lodash**: The benchmark uses Lodash's `_.keyBy()` function to create the dictionary. Lodash is a popular JavaScript library that provides a collection of functional programming helpers and utility functions. **Special JS feature or syntax:** The benchmark does not use any special JavaScript features or syntax beyond what's standard in modern JavaScript. **Other alternatives:** If you're looking for alternative approaches, consider the following: 1. **Splice()**: Instead of using `Array.includes()` or dictionary lookup, you could use `splice()` to find and remove the element from the array. 2. **Set data structure**: If you need to frequently search for elements in an array, consider converting it to a Set data structure, which provides faster lookups. Keep in mind that these alternatives might have different trade-offs in terms of memory usage, complexity, and performance compared to the original approaches used in the benchmark.
Related benchmarks:
array to object by key with lodash
object to array with lodash
my test lodash vs native
Array.includes vs Dictionary lookup no console
Comments
Confirm delete:
Do you really want to delete benchmark?