Optimize Array.indexOf How can I optimize checking a value in recursion before operating on it? I'm using Array.indexOf (this is Javascript)

var nums = [], lengths = [];
function findNumLength(n) {
    //preliminary check to see if 
    //we've done this number before
    var indexOf = nums.indexOf(n);
    if(indexOf !== -1) {
        return lengths[indexOf];
    }
    function even (n2) { return n2%2===0; }
    if(n===1) {
        return 1;
    }
    if(even(n)) {
        l = findNumLength(n/2) + 1;
        if(indexOf===-1) { 
            lengths.splice(0,0 ,l);
            nums.push(n);
        }
        return l;
    }
    else {
        l = findNumLength(3*n + 1) + 1;
        if(indexOf===-1){
            lengths.splice(0,0,l);
            nums.push(n);
        }
        return l;
    }
}


(note: I've answered my own question with one solution I've found; it is by no means the only solution (though it may be the best. I don't know). Please, still answer.)
The faster way to know if a number is already in a data structure is to use an object, not an array.  So, if you don't have to have the numbers in an array, then this would be a lot faster, particular when the number of items gets big because the object look up is massively faster than the linear search of .indexOf():

var numbers = {};
function recurse(newNumber) {
    if(newNumber in numbers) {
        return newNumber;
    }
    //do stuff to number
    numbers[newNumber] = true;
    return recurse(modified_number);
}


You can then iterate through the items in numbers like this:

for (var num in numbers) {
    if (numbers.hasOwnProperty(num) {
        // process num here
    }
}


FYI, if speed is really your goal here, recursion is not the fastest way to do this (because functions calls are kind of slow in javascript), particularly if you don't have much or any local state in the function so you could just use a while loop of sorts.