I looked through submitted answers and got that they only apply if you search for the object via reference. A simple linear search with reference object comparison.
But lets say you don't have the reference to an object, how will you find the correct object in the array? You will have to go linearly and deep compare with each object. Imagine if the list is too large, and the objects in it are very big containing big pieces of text. The performance drops drastically with the number and size of the elements in the array.
You can stringify objects and put them in the native hash table, but then you will have data redundancy remembering these keys cause JavaScript keeps them for 'for i in obj', and you only want to check if the object exists or not, that is, you have the key.
I thought about this for some time constructing a JSON Schema validator, and I devised a simple wrapper for the native hash table, similar to the sole hash table implementation, with some optimization exceptions which I left to the native hash table to deal with. It only needs performance benchmarking...
All the details and code can be found on my blog: http://stamat.wordpress.com/javascript-quickly-find-very-large-objects-in-a-large-array/
I will soon post benchmark results.
The complete solution works like this:
var a = {'a':1,
'b':{'c':[1,2,[3,45],4,5],
'd':{'q':1, 'b':{'q':1, 'b':8},'c':4},
'u':'lol'},
'e':2};
var b = {'a':1,
'b':{'c':[2,3,[1]],
'd':{'q':3,'b':{'b':3}}},
'e':2};
var c = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
var hc = new HashCache([{a:3, b:2, c:5}, {a:15, b:2, c:'foo'}]); //init
hc.put({a:1, b:1});
hc.put({b:1, a:1});
hc.put(true);
hc.put('true');
hc.put(a);
hc.put(c);
hc.put(d);
console.log(hc.exists('true'));
console.log(hc.exists(a));
console.log(hc.exists(c));
console.log(hc.exists({b:1, a:1}));
hc.remove(a);
console.log(hc.exists(c));
~[1,2,3].indexOf(4)
will return 0 which will evaluate as false, whereas~[1,2,3].indexOf(3)
will return -3 which will evaluate as true. – lordvlad Oct 2 '13 at 7:59~
is not what you want to use to convert to a boolean, for that you need!
. But in this case you want to check equality with -1, s o the function might endreturn [1,2,3].indexOf(3) === -1;
~
is a binary not, it will invert each bit of the value individually. – mcfedr Jun 20 '14 at 12:49[1,2,3].indexOf(4)
will actually return -1. As @mcfedr pointed out,~
is the bitwise-NOT operator, see ES5 11.4.8. Thing is, since the binary representation of-1
consists of only 1's, it's complement is0
, which evaluates as false. The complement of any other number will be non-zero, hence true. So,~
works just fine and is often used in conjunction withindexOf
. – mknecht Mar 14 '15 at 5:35[[1,2],[3,4]].includes([3,4])
? – mplungjan Apr 2 '17 at 9:20