Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array find() vs js object
(version: 1)
Comparing performance of:
prepare test vs object vs array find
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function makeid(length) { var result = ''; var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var charactersLength = characters.length; for ( var i = 0; i < length; i++ ) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } var dataArr = []; var dataObj = []; for (var i = 0; i < 80000; i++) { var data = { id: i, displayName: makeid(5) }; dataArr.push(data); dataObj[i] = data; } var toDisplay = []; for (var i = 79000; i < 80000; i++) { toDisplay.push(i) } var toDisplaySet = new Set(toDisplay);
Tests:
prepare test
[...toDisplaySet].map(record => dataArr.find(e => e.id === record));
object
[...toDisplaySet].map(record => dataObj[record]);
array find
[...toDisplaySet].map(record => dataArr.find(e => e.id === record));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
prepare test
object
array find
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0
Browser/OS:
Firefox 124 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
prepare test
1.8 Ops/sec
object
31722.2 Ops/sec
array find
2.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark compares the performance of three different approaches: 1. `prepare test`: This approach uses an array-based method to find the corresponding object from the `dataObj` array using a two-pointer technique. 2. `object`: This approach uses the built-in JavaScript method `find()` on the `dataArr` array and then accesses the object by its index (i.e., `dataObj[i]`). 3. `array find`: This approach is identical to the previous one, where `[...toDisplaySet].map(record => dataArr.find(e => e.id === record))` uses the `find()` method on the `dataArr` array. **Libraries and Frameworks** None of the benchmark code explicitly uses any external libraries or frameworks. However, it relies on JavaScript's built-in methods like `Array.prototype.find()` and `Set`. **Special JS Features and Syntax** There are no special JavaScript features or syntax used in this benchmark that would require a deep understanding of advanced JavaScript concepts. Now, let's discuss the pros and cons of each approach: 1. **prepare test**: This approach uses a two-pointer technique to find the corresponding object from the `dataObj` array. The benefits include: * O(1) lookups for the `toDisplaySet`, making it efficient. * Avoids using the `find()` method, which has an average time complexity of O(n). * However, this approach is more complex and might be harder to understand. 2. **object**: This approach uses the built-in `find()` method on the `dataArr` array and then accesses the object by its index (i.e., `dataObj[i]`). The benefits include: * Simple and easy to understand. * Uses a well-known JavaScript method, making it accessible to developers familiar with JavaScript. * However, this approach has an average time complexity of O(n) due to the `find()` method, which might not be as efficient for large datasets. 3. **array find**: This approach is identical to the previous one (`prepare test`), and its pros and cons are similar. **Other Alternatives** If you were to rewrite this benchmark with alternative approaches, some options could include: * Using a hash table or object-based data structure instead of an array for faster lookups. * Utilizing a more efficient algorithm like binary search or interpolation searching. * Experimenting with different optimization techniques, such as caching or parallel processing. Keep in mind that these alternatives would require significant changes to the benchmark code and might not be applicable depending on the specific requirements of the use case.
Related benchmarks:
JS Some vs Includes
Map.get() vs Array.find() - With the same value to search
orderBy vs array.prototype.sort vs vanila orderBy vs QuickSort
array find vs object key
Find Object ID vs IndexOf int
Comments
Confirm delete:
Do you really want to delete benchmark?