Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find index in array performance
(version: 0)
for of, for, indexOf
Comparing performance of:
indexOf vs for vs for of
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]; var letters = ['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 mixed = [ 1, 'b', 3, 'd', 5, 'f', 7, 'h', 9, 'j', 11, 'l', 13, 'n', 15, 'p', 17, 'r', 19, 't', 21, 'v', 23, 'x', 25, 'z' ]; function indexByIndexOf (array, value) { return array.indexOf(value) } function indexByFor (array, value) { for (var i=0; i < array.length; i++) { if(array[i] === value) { return i; } } } function indexByForOf (array, value) { var i = 0; for (var currVal of array) { i++; if(currVal === value) { return i; } } }
Tests:
indexOf
var numberindex = indexByIndexOf(numbers, 27); var letterindex = indexByIndexOf(letters, 'x'); var mixedIndex = indexByIndexOf(mixed, 'x');
for
var numberindex = indexByFor(numbers, 27); var letterindex = indexByFor(letters, 'x'); var mixedIndex = indexByFor(letters, 'x');
for of
var numberindex = indexByForOf(numbers, 27); var letterindex = indexByForOf(letters, 'x'); var mixedIndex = indexByForOf(letters, 'x');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
indexOf
for
for of
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):
I'll break down the provided JSON and explain what's being tested, compared, and their pros/cons. **Benchmark Definition:** The benchmark measures the performance of three different ways to find an index in an array: 1. `indexByIndexOf`: Uses the built-in `indexOf` method of arrays. 2. `indexByFor`: Iterates through the array using a traditional for loop. 3. `indexByForOf`: Iterates through the array using the `for...of` loop syntax. **Script Preparation Code:** The script preparation code creates three arrays: * `numbers`: An array of integers from 1 to 30. * `letters`: An array of lowercase letters from 'a' to 'z'. * `mixed`: An array containing a mix of integers, strings, and other data types. **Function Definitions:** Three functions are defined to perform the three different methods: 1. `indexByIndexOf`: Takes two arguments (`array` and `value`) and returns the index of the first occurrence of `value` in `array`. 2. `indexByFor`: Takes two arguments (`array` and `value`) and iterates through the array using a traditional for loop to find the index of the first occurrence of `value`. 3. `indexByForOf`: Takes two arguments (`array` and `value`) and uses the `for...of` loop syntax to iterate through the array, incrementing an index variable, and returns the index when the condition is met. **Individual Test Cases:** Three test cases are defined: 1. `indexOf`: Measures the performance of each method on the entire `numbers`, `letters`, and `mixed` arrays with a value of 27 (for `numbers`) and 'x' (for `letters` and `mixed`). 2. `for`: Measures the performance of each method on the entire `letters` array with a value of 'x'. 3. `for of`: Measures the performance of each method on the entire `letters` array with a value of 'x'. **Pros and Cons:** * `indexByIndexOf`: + Pros: Built-in method, likely to be faster due to optimization by the engine. + Cons: May not be suitable for arrays where the element is not found (returns -1). * `indexByFor`: + Pros: Control over iteration and potential for better performance in certain cases. + Cons: Less efficient than built-in methods, especially with large arrays. * `indexByForOf`: + Pros: Newer syntax, likely to be faster due to its optimized implementation by the engine. + Cons: Less intuitive and less control over iteration. **Other Considerations:** The benchmark does not consider other factors that might impact performance, such as: * Array size: The current script only uses relatively small arrays. Increasing array sizes would affect performance significantly. * Data type: Using larger or more complex data types (e.g., objects) might change the results. * Browser-specific optimizations: Different browsers may optimize certain methods differently. **Alternative Benchmarks:** Other benchmarks could measure: * Performance of different sorting algorithms (e.g., `sort()`, `Array.prototype.sort()`). * Performance of array manipulation operations (e.g., push, pop, splice, filter). * Performance of string processing operations (e.g., substring, replace). Keep in mind that these alternatives would require additional script preparation and function definitions to accurately represent the desired scenarios.
Related benchmarks:
set.has vs. array.includes vs array.indexOf (string values) (larger array)
Set vs array indexOf vs array inclues
indexof vs set123
index vs map111
Comments
Confirm delete:
Do you really want to delete benchmark?