Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For Loop vs Slice for searching for needle in haystack in small array
(version: 1)
Comparing performance of:
For loop vs Slice
Created:
9 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const arraySize = 200; // Size of the array const array = Array.from({ length: arraySize }, (_, i) => i); // Array of numbers from 0 to arraySize - 1 const randomIndex = Math.floor(Math.random() * (arraySize - 1)); // Random index in the array const needle = array[randomIndex + Math.floor(Math.random() * (arraySize - randomIndex - 1)) + 1]; // Needle after the random index
Tests:
For loop
let foundIndex = -1; for (let i = randomIndex + 1; i < array.length; i++) { if (array[i] === needle) { foundIndex = i; break; } }
Slice
const slicedArray = array.slice(randomIndex + 1); const foundIndexInSlice = slicedArray.indexOf(needle); const foundIndex = foundIndexInSlice !== -1 ? foundIndexInSlice + randomIndex + 1 : -1;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
For loop
Slice
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:141.0) Gecko/20100101 Firefox/141.0
Browser/OS:
Firefox 141 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For loop
26566930.0 Ops/sec
Slice
9045150.0 Ops/sec
Related benchmarks:
array vs set index deletion
Find Smallest Number Not In An Array
Splice vs new Array
Splice vs new Array Fix
Array.at(i) vs Array[i]
Array timing
Array timing 100
100 array
For Loop vs Slice for searching for needle in haystack in 20 length array
Comments
Confirm delete:
Do you really want to delete benchmark?