Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
For Loop vs Slice for searching for needle in haystack in small array
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:141.0) Gecko/20100101 Firefox/141.0
Browser:
Firefox 141
Operating system:
Mac OS X 10.15
Device Platform:
Desktop
Date tested:
9 months ago
Test name
Executions per second
For loop
26566930.0 Ops/sec
Slice
9045150.0 Ops/sec
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;