Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find vs lookup
(version: 0)
Comparing performance of:
find method vs lookup method
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 10) arr[i] = ('' + i++); var testSet = new Set(arr); var testObj = {}; for (const a of arr) { testObj[a] = true; } var randomKeys = []; i = 0; while (i <= 50) { randomKeys.push(''+Math.round(Math.random(50000) + 5000)); i++; }
Tests:
find method
for (const key of randomKeys) {const index = arr.find(item => item == key);}
lookup method
for (const key of randomKeys) {const index = testObj[key];}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
find method
lookup method
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Browser/OS:
Chrome 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
find method
1434799.5 Ops/sec
lookup method
5242896.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this JavaScript microbenchmark. **What is being tested?** The benchmark compares the performance of two methods: `arr.find()` and `testObj[key]`. Both methods are used to find an element in an array or object, respectively. The test creates a large array `arr` with 11 elements, each assigned a unique value between 0 and 10. It also creates a `Set` object from `arr` and an object `testObj` where each key is an element of `arr` and the corresponding value is `true`. **Options being compared** The two options being compared are: 1. **`arr.find()`**: This method searches for the first occurrence of a given element in the array. 2. **`testObj[key]`**: This approach assumes that if the key exists in the object, its value will be truthy. **Pros and Cons** * `arr.find()`: + Pros: Simple and efficient way to find an element in an array. It returns the first matching element or `-1` if no match is found. + Cons: May have poor performance for large arrays because it iterates over each element until a match is found. * `testObj[key]`: + Pros: Can be faster than `arr.find()` because it only checks for the existence of the key in the object, without iterating over all elements. However, this approach relies on the presence of the key being truthy. + Cons: May not work correctly if the key is not present in the object or its value is falsy. **Library and its purpose** The `Set` object is a built-in JavaScript library that provides an efficient way to store unique values. Its primary purpose is to eliminate duplicates while maintaining fast lookup, insertion, and deletion operations. **Special JS feature or syntax** There is no special JS feature or syntax being used in this benchmark. It's purely functional programming with standard JavaScript primitives. **Other alternatives** If you were to rewrite the `arr.find()` method using a different approach, such as iterating over all elements of the array, it would likely perform poorly for large arrays. Another alternative could be using a data structure like a `Map` or a binary search tree (BST) for faster lookups. In summary, this benchmark compares two methods for finding an element in an array and object: the built-in `arr.find()` method and a custom approach relying on the existence of the key being truthy. The choice between these options depends on performance requirements and specific use cases.
Related benchmarks:
find by array loop vs Array.find
String in Array: Set vs IndexOf vs includes vs findIndex vs find
String in Array: Set vs IndexOf vs includes vs findIndex vs find v2
Object lookup vs array lookup
Comments
Confirm delete:
Do you really want to delete benchmark?