01 / 11
Same answer, speed differing by orders of magnitude
When you looked for a value in an array in JavaScript, you ran a for loop and went through from the front. That does give the right answer.
But being right and being fast are different matters. With ten elements, any way of searching finishes instantly. With a million it is another story. Going through from the front means up to a million comparisons in the worst case — yet given the right conditions there is a way to find it in twenty.
What this lesson is for is not memorising the fine steps but growing an eye for how the effort grows as the data grows. Does ten times the data take ten times as long, or a hundred times? See that and you can catch code before it becomes "works but slow".
when n = 1,000,000linear search 1,000,000 stepsbinary search 20 steps