Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find array
(version: 0)
Comparing performance of:
sort manual idx vs sort findindex vs map find
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const shuffleArray = array => { const arr = [...array]; for (let i = arr.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); const temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } return arr; } var testArr = Array.from({ length: 5000 }, () => Math.floor(Math.random() * 4000)); var scrambled = shuffleArray(testArr);
Tests:
sort manual idx
scrambled.sort((a,b) => { let idx1, idx2; for (let i = 0; i < testArr.length;i++) { if (testArr[i] === a) { idx1 = i if (idx2 != null) break } else if (testArr[i] === b) { idx2 = i if (idx1 != null) break } } return idx1 - idx2 });
sort findindex
scrambled.sort((a,b) => testArr.findIndex(t => t === a) - testArr.findIndex(t => t === b));
map find
testArr.map((t) => scrambled.find(s => s === t));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
sort manual idx
sort findindex
map find
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 what's being tested in this JavaScript microbenchmark: **Benchmark Definition** The benchmark is defined by the `Benchmark Definition` JSON, which provides two scripts: `Script Preparation Code` and `Html Preparation Code`. The `Script Preparation Code` defines a function called `shuffleArray`, which shuffles an array of 5000 random numbers. This shuffled array is then stored in the variable `scrambled`. The benchmark itself consists of three test cases: 1. **Manual Indexing**: The first test case uses a manual indexing approach to find the index of each element in the sorted array. 2. **`findIndex` Method**: The second test case uses the `findIndex` method of the Array prototype, which returns the index of the first occurrence of a specified value. 3. **`map` Function with `find` Method**: The third test case uses the `map` function to create a new array with the elements in the same order as they appear in the original array, and then uses the `find` method to find the corresponding index. **Options Compared** The three test cases are compared using different approaches: 1. **Manual Indexing**: This approach requires iterating through the sorted array to find the indices of each element. 2. **`findIndex` Method**: This approach is more efficient, as it uses a binary search algorithm to find the index of the first occurrence of a specified value. 3. **`map` Function with `find` Method**: This approach is even more efficient, as it avoids the need for manual indexing altogether and uses a single pass through the array. **Pros and Cons** Here are some pros and cons of each approach: 1. **Manual Indexing**: * Pros: Simple to implement and understand. * Cons: Inefficient, especially for large arrays. 2. **`findIndex` Method**: * Pros: More efficient than manual indexing, with a time complexity of O(n). * Cons: Requires the use of the `findIndex` method, which may not be supported in older browsers. 3. **`map` Function with `find` Method**: * Pros: Most efficient approach, with a time complexity of O(n), and avoids manual indexing altogether. * Cons: May require additional dependencies or polyfills for older browsers. **Other Considerations** The benchmark also takes into account various device platforms (Desktop, Mobile), operating systems (Mac OS X 10.15.7), and browser versions (Chrome 109). In terms of special JavaScript features, none are mentioned in the provided code. However, it's worth noting that some of these approaches may require support for modern JavaScript features or polyfills. **Alternatives** If you're interested in exploring alternative approaches, here are a few options: 1. **Using `Array.prototype.findIndex` with a custom implementation**: You could implement your own binary search algorithm using `findIndex` and compare it to the existing benchmark. 2. **Using `Array.prototype.forEach` with manual indexing**: You could use the `forEach` method instead of `map` and manually update an index variable during iteration. 3. **Using a different sorting algorithm**: Depending on the specific requirements of your use case, you might consider using a different sorting algorithm, such as Quicksort or Mergesort. Keep in mind that these alternatives may have varying levels of efficiency and complexity compared to the existing benchmark.
Related benchmarks:
Already sorted versus random
Array.Sort vs Math.Min-Max
Array.sort vs Array.map
Array.sort vs Array.map x1
Comments
Confirm delete:
Do you really want to delete benchmark?