最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Checking if element exists in array without iterating through it - Stack Overflow

programmeradmin5浏览0评论

my array:

tempListArray = "[{"id":"12","value":false},{"id":"10","value":false},{"id":"9","value":false},{"id":"8","value":false}]";

To check if an element exists I would do this:

for (var i in tempListArray) {
    //check flag
    if (tempListArray[i].id == Id) {
        flagExistsLoop = 1;
        break;
    }
} 

Is there anyway, I can check if an Id exists without looping through the whole array. Basically I am worried about performance if say I have a 100 elements.

Thanks

my array:

tempListArray = "[{"id":"12","value":false},{"id":"10","value":false},{"id":"9","value":false},{"id":"8","value":false}]";

To check if an element exists I would do this:

for (var i in tempListArray) {
    //check flag
    if (tempListArray[i].id == Id) {
        flagExistsLoop = 1;
        break;
    }
} 

Is there anyway, I can check if an Id exists without looping through the whole array. Basically I am worried about performance if say I have a 100 elements.

Thanks

Share Improve this question edited Oct 16, 2014 at 22:23 Dave 10.9k3 gold badges44 silver badges54 bronze badges asked Oct 16, 2014 at 21:50 bobo2000bobo2000 1,8777 gold badges34 silver badges54 bronze badges 3
  • 1 100 elements? That's nothing really. Don't worry about it. – elclanrs Commented Oct 16, 2014 at 21:52
  • 1 for...in loops are a bad way of iterating arrays. – Oriol Commented Oct 16, 2014 at 21:54
  • Check out this post I think it is what you need. – aabiro Commented Jul 19, 2018 at 15:52
Add a ment  | 

6 Answers 6

Reset to default 2

No, without using custom dictionary objects (which you seriously don't want to for this) there's no faster way than doing a 'full scan' of all contained objects.

As a general rule of thumb, don't worry about performance in any language or any situation until the total number of iterations hits 5 digits, most often 6 or 7. Scanning a table of 100 elements should be a few milliseconds at worst. Worrying about performance impact before you have noticed performance impact is one of the worst kinds of premature optimization.

No, you can't know that without iterating the array.

However, note for...in loops are a bad way of iterating arrays:

  • There is no warranty that it will iterate the array with order
  • It will also iterate (enumerable) non-numeric own properties
  • It will also iterate (enumerable) properties that e from the prototype, i.e., defined in Array.prototype and Object.protoype.

I would use one of these:

  • for loop with a numeric index:

    for (var i=0; i<tempListArray.length; ++i) {
        if (tempListArray[i].id == Id) {
            flagExistsLoop = 1;
            break;
        }
    } 
    
  • Array.prototype.some (EcmaScript 5):

    var flagExistsLoop = tempListArray.some(function(item) {
        return item.id == Id;
    });
    

    Note it may be slower than the other ones because it calls a function at each step.

  • for...of loop (EcmaScript 6):

    for (var item of tempListArray) {
        if (item.id == Id) {
            flagExistsLoop = 1;
            break;
        }
    } 
    

Depending on your scenario, you may be able to use Array.indexOf() which will return -1 if the item is not present.

Granted it is probably iterating behind the scenes, but the code is much cleaner. Also note how object parisons are done in javascript, where two objects are not equal even though their values may be equal. See below:

var tempListArray = [{"id":"12","value":false},{"id":"10","value":false},{"id":"9","value":false},{"id":"8","value":false}];

var check1 = tempListArray[2];
var check2 = {"id":"9","value":false};

doCheck(tempListArray, check1);
doCheck(tempListArray, check2);

function doCheck(array, item) {
  var index = array.indexOf(item);
  
  if (index === -1)
    document.write("not in array<br/>");
  else
    document.write("exists at index " + index + "<br/>");
}

try to use php.js it may help while you can use same php function names and it has some useful functionalities

There is no way without iterating through the elements (that would be magic).

But, you could consider using an object instead of an array. The object would use the (presumably unique) id value as the key, and the value could have the same structure you have now (or without the redundant id property). This way, you can efficiently determine if the id already exists.

There is a possible cheat for limited cases :) and it is magic...cough cough (math)

imagine you have 3 elements:

  • 1
  • 2
  • 3

and you want to know if one of these is in an array without iterating it...

we could make a number that contains a numerical flavor of the array. we do this by assigning prime numbers to the elements:

  • 1 - 2
  • 2 - 3
  • 3 - 5

the array so when we add item 2 we check that the array doesn't already contain the prime associated to that item by checking (if Flavor!=0 && (Flavor%3)!=0) then adding the prime Flavor*=3;

now we can tell that the second element is in the array by looking at the number.

if Flavor!=0 && (Flavor%3)==0 // its There!

Of course this is limited to the numerical representation that can be handled by the puter. and for small array sizes (1-3 elements) it might still be faster to scan. but it's just one idea.

but the basis is pretty sound. However, this method bees unusable if you cannot correlate elements one to one with a set of primes. You'll want to have the primes calculated in advance. and verify that the product of those is less numerical max numerical representation. (also be careful with floating-point. because they might not be able to represent the number at the higher values due to the gaps between representable values.) You probably have the best luck with an unsigned integer type.

This method will probably be too limiting. And there is something else you can do to possibly speed up your system if you don't want to iterate the entire array.

Use different structures: dictionaries/maps/trees etc.

if your attached to the array another method can be a bloom filter. This will let you know if an element is not in your set, which can be just as useful.

发布评论

评论列表(0)

  1. 暂无评论