Quick actions

cmd+k|ctrl+k

Navigation

Languages

Find Nemo Javascript in array Big O

Snippet info

Language

JavaScript

Visibility

public

Author

benjamin.vander

Created

2025-10-06T19:38:22.4534Z

Updated

2025-10-06T21:49:15.553163Z


const CNT_NEMO = 'nemo'
const nemo = [CNT_NEMO];
const everyone = ['dory', 'bruce', 'marlin', CNT_NEMO, 'gill', 'bloat', 'nigel', 
'squirt', 'darla', 'hank'];
const large = new Array(20000000).fill('gill');
large.push(CNT_NEMO);
function findNemo(array){
    let t0 = performance.now();
    for(let i = 0; i < array.length ; i++) {
        if(array[i] === CNT_NEMO){
            console.log('Found NEMO!');
        }
    }
    let t1 = performance.now();
    console.log('call to find Nemo took ' +(t1-t0)+ ' millisends')
}

findNemo(everyone); 
//  O(n) --> Linear Time
// with nemo     =>  O(1)
// with everyone =>  O(10)
// with large    =>  O(20000000)


const boxes = [0,1,2,3,4,5]

function logFirstTwoBoxes(boxes) {
    console.log(boxes[0]); // O(1)
    console.log(boxes[1]); // O(1)
}

logFirstTwoBoxes(boxes); // O(2)

INFO